简体   繁体   English

Selenium / python-无法找到部分链接文本

[英]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 ): 这是一个示例( WO20href后面):

<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>

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.

相关问题 Selenium/python - “查找部分链接文本”和“xpath” - Selenium/python - “find partial link text ” and “xpath” Selenium Python查找元素部分链接文本 - Selenium Python find element partial link text Python Selenium 从列表中查找部分链接文本 - Python Selenium Find Partial Link Text from a List find_elements_by_partial_link_text(link_text)硒Python - find_elements_by_partial_link_text(link_text) Selenium Python 硒无法通过部分链接文本找到链接 - Selenium won't find link by partial link text Python Selenium从find_elements_by_partial_link_text中提取href信息 - Python Selenium pull href info out of find_elements_by_partial_link_text selenium (python) 引发 StaleElementReferenceException 并且不会继续下载所有 webdriver.find_elements_by_partial_link_text() - selenium (python) raises StaleElementReferenceException and does not continue to download all webdriver.find_elements_by_partial_link_text() Selenium (browser.find_element(by=By.PARTIAL_LINK_TEXT) 错误 - Selenium (browser.find_element(by=By.PARTIAL_LINK_TEXT) error Selenium:在find_element_by_partial_link_text()中使用多个字符串 - Selenium: Use Multiple Strings in find_element_by_partial_link_text() 在 chrome 无头模式下使用 python selenium 定位部分链接文本 - Locating partial link text with python selenium in chrome headless mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM