问题
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.
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?
回答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"
回答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)']"));
来源:https://stackoverflow.com/questions/16757770/using-selenium-xpath-cannot-find-text-element