2

I'm new to Selenium and have a problem to find an element by XPath. I was trying to handle this issue but spent too much time on it and it still doesn't work.

I'm trying to automate file downloading from this site.

I'm searching for the element which contain version name and afterwards I need to click the download icon accordingly.

example

I'm writing the following code in order to find element

var element1 = driver.FindElement(By.XPath("//*[@id='versiontable']/div[3]/a[Text()='Firefox 22.0 (Beta 2)']"));

However, it doesn't work, element cannot be found.

And after I need to click a download icon accordingly. I'm not sure how to click element which is relevant for current version of firefox. Any suggestions?

2
  • The XPath looks wrong. Try Firepath or some similar tool to be able to check your XPath expressions in Firebug (or use the Firebug console). Also, I'd try //*[@id='versiontable']/div[3]//a[text()='Firefox 22.0 (Beta 2)'] (uses //a to match deeper a elements - not sure about the div[3] part, though) or rather By.linkText(). Commented May 26, 2013 at 9:34
  • Thanks for advice. I downloaded Firepath and it seems very helpful. Commented May 26, 2013 at 10:51

2 Answers 2

4

Why don't you use By.linktext, seems much easier to me?

driver.findElement(By.linkText("Firefox 22.0 (Beta 2)")).click(); 

However, the reason your XPath is not working is that it does not fit the structure of the webpage. div[3] does not mean three divs in a hierarchy one after another, but it means the third divin the next hierarchy level. You would need something like this instead

var element1 = driver.FindElement(By.XPath("//*[@id='versiontable']/div/div/div/a[text()='Firefox 22.0 (Beta 2)']"));
Sign up to request clarification or add additional context in comments.

1 Comment

You are right, it's actually working - just linkText. However, I'm sure I was trying it several times, but anyway, it does work for me now. Thanks.
1

First of all the xpath function text() has to written in lowercase.
Also the link entry you are looking for is not the third (3) div.

Because the direct download is the link in the div with class="right".
Please try this:

"//div[div/a[text()='Firefox 22.0 (Beta 2)']]/div[@class='right']/a"

6 Comments

This work as well for me. Thanks! I just wanted to clarify why we need bracket "[" after first div?
This is not very efficient nor maintainable. You are using the same expression as cindition and then for navigation, so you are doing the lookup twice for no reason. It is the same as //div/div[@class='right']/a[text()='Firefox 22.0 (Beta 2)'] in this case. In case there would be multiple a elements within the div it would not even be correct as it would find all a elements then.
@dirkk: I fear you are missing the point here that there are two links in the "row". I suggest to have a closer look to the page. IF you still do not understand the xapth cam back an I will give you the right explanation;-). Also perhaps you should test your xpaht.
@hr_117: please can you explain why we need bracket "[" after first div? sorry for question, just want to understand and use it next time.
This are "predicates". Predicates are always embedded in square brackets. The predicate (bracket "[" after first div) is used to find the "row" you are looking for. (which has a link for "Firefox 22.0 (Beta 2)" in left "column" ) for his "row" wie select the right "column" link. An alternative may be "//div/div/a[text()='Firefox 22.0 (Beta 2)']/../../div[@class='right']/a. Select the left link go to steps (parents) back and select the "right" one. (I prefer the first solution.)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.