Using Selenium, xpath cannot find 'text' element

13,365

Solution 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"

Solution 2

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)']"));
Share:
13,365
u_untouchable
Author by

u_untouchable

Updated on June 05, 2022

Comments

  • u_untouchable
    u_untouchable almost 2 years

    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?

  • u_untouchable
    u_untouchable almost 11 years
    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.
  • u_untouchable
    u_untouchable almost 11 years
    This work as well for me. Thanks! I just wanted to clarify why we need bracket "[" after first div?
  • dirkk
    dirkk almost 11 years
    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.
  • hr_117
    hr_117 almost 11 years
    @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.
  • u_untouchable
    u_untouchable almost 11 years
    @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.
  • hr_117
    hr_117 almost 11 years
    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.)
  • u_untouchable
    u_untouchable almost 11 years
    @hr_117: Thanks a lot for explanation
  • dirkk
    dirkk almost 11 years
    @hr_117 Ah, I see now want you wanted to achieve, you are right. If you want to click the download link on the right your solution is obviously correct. Based on the original question I though the version name link was searched, but I see this was just the identifier.