Pasukaru
Pasukaru

Reputation: 1095

Selenium Firefox/Web-Driver can't find element by XPath

I'm having trouble reading a link inside a div. Ok, here's what the div looks like:

<div id="AjaxStream" style="clear: both">
<a target="_blank" href="http://www.something.com/">
<img height="370" width="752" border="4" usemap="#Link" src="somefile.png">
</a>
</div>

The following code, to find the div works perfectly fine. (I tried element.getAttribute("id") - which returned "AjaxStream")

WebElement element = river.findElement(By.xpath("//html/body/div/div[2]/div/div[11]"));

And here is what's not working:

WebElement element = driver.findElement(By.xpath("//html/body/div/div[2]/div/div[11]/a"));

This should actually fine the link-element, but it doesn't. Any ideas? Thanks in advance.

##Edit: Nevermind - I fixed it. The problem was that the element wasn't loaded. I added a Thread.sleep(1000) before trying to find the element - and now it works perfectly fine.

Upvotes: 0

Views: 4572

Answers (2)

axatrikx
axatrikx

Reputation: 396

try

WebElement element = driver.findElement(By.xpath("//div[@id='AjaxStream']/a"));
String link = element.getAttribute("href");

//I need 6 characters but it's a 1 "char" fix

Upvotes: 1

Tommy Chan
Tommy Chan

Reputation: 622

Have a look at your xpath...to me that is unreadable. If someone comes in later in a few months, are they able to translate that xpath in to tag you're looking for? A better solution would be a to add an id attribute to the tag you are interested, and find it by that ID.

Upvotes: 0

Related Questions