3

i am using this code but it returns me the name of the image name with the url

IWebElement element = driver.FindElement(By.XPath("Your xpath"));
string path = element.GetAttribute("src");

result:

http://nameofthehost/imagename.jpg

what i would like is just to spit me the src name, how would i do that?

2 Answers 2

5

Some browsers return the full URL in JavaScript, even if a relative URL is specified in the HTML source. So that you don't have to special-case your code based on the browser you're using WebDriver normalizes all properties and attributes that contain URLs to return the full URL. To solve the problem, you can use JavaScript directly.

// assume driver is a valid WebDriver object
// Java code
WebElement element = driver.findElement(By.xpath("your XPath"));
String src = ((JavascriptExecutor)driver).executeScript("return arguments[0].attributes['src'].value;", element).toString();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks I will give a shot and since you are the core contributor to Selenium would you answer this question please? stackoverflow.com/questions/12151958/…
@JimEvans - Thanks for your answer, it solved my problem too, i am getting complete src name, i want only the text/value displayed in the image source. Can you give me any resolution for getting on the value displayed on the image. please find the string i retrieved from the code. "QuickApp/HumanValidationImage.aspx?tid=LZhU3a". But i want only the value "LZhU3a"(Auto Generated). Help will be appreciated
@UmamaheshwarThota to extract the value you need, use following code: scrID = "QuickApp/HumanValidationImage.aspx?tid=LZhU3a" scrID = scrID.substring(scrID.lastIndexOf('=') + 1); just fit it to your needs.
@Siemic -- Thanks for the response. My issue resolved long back. I have split the string as per my requirements and solved the problem.
1

In Generalized Form

WebElement element = driver.findElement(locator);
String src = element.getAttribute(src);
String [] srcs=src.split("\");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.