简体   繁体   English

使用Selenium,xpath找不到'text'元素

[英]Using Selenium, xpath cannot find 'text' element

I'm new to Selenium and have a problem to find an element by XPath. 我是Selenium的新手,无法通过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. 我不确定如何单击与当前版本的Firefox相关的元素。 Any suggestions? 有什么建议么?

Why don't you use By.linktext , seems much easier to me? 您为什么不使用By.linktext ,对我来说似乎容易得多?

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. 但是,您的XPath无法正常工作的原因是它不适合网页的结构。 div[3] does not mean three divs in a hierarchy one after another, but it means the third div in the next hierarchy level. div[3]并不表示一个层次结构中一个接一个的三个div,而是表示下一个层次结构中的第三个div 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)']"));

First of all the xpath function text() has to written in lowercase. 首先,xpath函数text()必须以小写形式编写。
Also the link entry you are looking for is not the third (3) div. 另外,您要查找的链接条目不是第​​三(3)格。

Because the direct download is the link in the div with class="right". 因为直接下载是div中class =“ right”的链接。
Please try this: 请尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM