Selenium/python-无法找到部分链接文本 [英] Selenium/python - fails to find partial link text
问题描述
运行Windows 10驱动程序(作为脚本和python REPL)的Windows 10上具有Python 2.7的硒无法通过部分链接文本找到元素,我不确定为什么.当我查看有问题的网页的源代码时,整个页面中只有一个WO20
实例,并且它在链接中,但是selenium返回no such element
.
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. 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
在href
之后):
Here's an example (the WO20
comes just after the 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>
页面上还有其他一些链接,但是我需要的是唯一一个字符组合为WO20
的链接,因此从理论上讲,这应该很容易识别.我的猜测是硒无法将其识别为链接,这就是partial_link_text
无法正常工作的原因.我尝试使用xpath
(成功),但是问题是返回表中的第n个链接,而我需要的链接(我正在处理多个文档)不在固定位置./p>
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. My guess is that selenium isn't recognizing this as a link, which is why partial_link_text
isn't working. 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.
推荐答案
实际上,没有包含WO20
的链接文本!我认为您将链接文本与<a>
元素的href
属性混淆了.您可以使用以下代码通过链接文本/部分链接文本找到目标元素:
Actually, there is no link text that contain WO20
! I think you confuse link text with href
attribute of <a>
element. You can find target element by link text/partial link text with following code:
driver.find_element_by_link_text('WO/2015/102036')
或
driver.find_element_by_partial_link_text('WO/20')
此XPaths
也应适用:
driver.find_element_by_xpath('//a[contains(@href, "WO20")]')
driver.find_element_by_xpath('//a[contains(text(), "WO/20")]')
这篇关于Selenium/python-无法找到部分链接文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!