Selenium/python-无法找到部分链接文本 [英] Selenium/python - fails to find partial link text

查看:144
本文介绍了Selenium/python-无法找到部分链接文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行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.

这是一个示例(WO20href之后):

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&amp;recNum=1&amp;office=&amp;queryString=FP%3A%28JP2014005719%29&amp;prevFilter=&amp;sortOption=Pub+Date+Desc&amp;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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆