Selenium find \u element \u by \u link \u text不适用于完整URL

2024-04-20 09:16:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个Python/Selenium程序,它可以生成一个我访问过的url文件,然后在我进行Google搜索时在浏览器中突出显示它们,这样我就不会再浪费时间访问这些站点了。你知道吗

#searched Google for: "county fair" site:etypeservices.com
element = driver.find_element_by_partial_link_text("etypeservices.com")
driver.execute_script("arguments[0].style.backgroundColor = 'yellow'", element)

到目前为止,还不错。但是如果我用同样的方法选择完整的网页。。。你知道吗

element = driver.find_element_by_link_text("http://archives.etypeservices.com/Wauneta1/Magazine132532/Publication/Magazine132532.pdf")

我得到以下错误:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element

你知道为什么会这样吗?你知道吗


Tags: 文件text程序comurlbydriverselenium
1条回答
网友
1楼 · 发布于 2024-04-20 09:16:05

根据find_element_by_partial_link_text函数文档:

Finds an element by a partial match of its link text.

Args:

link_text: The text of the element to partially match on.

假设您有以下链接定义:

<a href="http://example.com">Example Domain</a>

你可以这样搭配:

driver.find_element_by_partial_link_text("Example")

或者

driver.find_element_by_partial_link_text("Domain")

如果要通过元素的部分URL定位元素,则需要使用^{} attribute,可以使用XPath SelectorXPath contains()函数进行定位,如:

driver.find_element_by_xpath("//a[contains(@href,'http://example.com')]") 

相关问题 更多 >