Using Selenium, xpath cannot find 'text' element

梦想与她 提交于 2019-12-24 00:57:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!