简体   繁体   English

Selenium find_element_by_link_text无法用于完整网址

[英]Selenium find_element_by_link_text not working for full urls

I'm working on a Python/Selenium program that makes a file of urls I've visited, then highlights them in the browser when I do a Google Search so I don't waste time visiting these sites again. 我正在开发一个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)

So far, so good. 到现在为止还挺好。 But if I try to select the full webpage using the same method... 但是,如果我尝试使用相同的方法选择完整的网页...

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

I get the following error: 我收到以下错误:

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

Any ideas why this is happening? 任何想法为什么会这样?

As per find_element_by_partial_link_text function documentation: 根据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. link_text:要部分匹配的元素的文本。

Given you have the following link definition: 假设您具有以下链接定义:

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

You can match it like: 您可以像这样匹配它:

driver.find_element_by_partial_link_text("Example")

or 要么

driver.find_element_by_partial_link_text("Domain")

If you want to locate the element by its partial URL you need to use href attribute , it can be done using XPath Selector and XPath contains() function like: 如果要通过部分URL定位元素,则需要使用href属性 ,可以使用XPath SelectorXPath contains()函数来完成此操作,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM