[英]Selenium/python - fails to find partial link text
Selenium with Python 2.7 on Windows 10 running the Chrome driver (as a script and from the python REPL) fails to find an element by partial link text, and I'm not sure why. 在运行Chrome驱动程序的Windows 10上使用Python 2.7的Selenium(作为脚本并通过python REPL)无法通过部分链接文本找到元素,我不确定为什么。 When I look at the source code of the webpage in question, there is only one instance of
WO20
in the entire page, and it's in a link, but selenium returns no such element
. 当我查看有问题的网页的源代码时,整个页面中只有一个
WO20
实例,并且它在链接中,但是selenium no such element
返回no such element
。
Here's an example (the WO20
comes just after the href
): 这是一个示例(
WO20
在href
后面):
<a id="resultTable:0:resultListTableColumnLink" name="resultTable:0:resultListTableColumnLink" href="detail.jsf?docId=WO2015102036&recNum=1&office=&queryString=FP%3A%28JP2014005719%29&prevFilter=&sortOption=Pub+Date+Desc&maxRec=3" target="_self"><span class="notranslate"> WO/2015/102036</span></a>
The page has a few other links, but the one I need is the only one with the character combination WO20
, so in theory this should be easy to identify. 该页面还有其他一些链接,但是我需要的是唯一一个字符组合为
WO20
,因此从理论上讲,这应该很容易识别。 My guess is that selenium isn't recognizing this as a link, which is why partial_link_text
isn't working. 我的猜测是硒无法将其识别为链接,这就是为什么
partial_link_text
无法正常工作的原因。 I've tried using xpath
(successfully), but the problem is that that returns the n-th link in the table, and the links I need (I'm processing multiple documents) aren't in a fixed position. 我尝试成功使用
xpath
,但是问题是返回表中的第n个链接,而我需要的链接(我正在处理多个文档)不在固定位置。
Actually, there is no link text that contain WO20
! 实际上,没有包含
WO20
链接文本! I think you confuse link text with href
attribute of <a>
element. 我认为您将链接文本与
<a>
元素的href
属性混淆了。 You can find target element by link text/partial link text with following code: 您可以使用以下代码通过链接文本/部分链接文本找到目标元素:
driver.find_element_by_link_text('WO/2015/102036')
or 要么
driver.find_element_by_partial_link_text('WO/20')
this XPaths
should be applicable also: 此
XPaths
也应适用:
driver.find_element_by_xpath('//a[contains(@href, "WO20")]')
driver.find_element_by_xpath('//a[contains(text(), "WO/20")]')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.