如何使用selenium python通过href值查找元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33155454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:53:07  来源:igfitidea点击:

How to find an element by href value using selenium python?

pythonselenium

提问by user3870509

I have href value of an anchor tag which only have href value as attribute. Now I want to find the element in the page which have same value as my href value and click it. I am unable to find any way of doing this using standard selenium methods.How can I do this? Basically these are the functions I found but it seems that I can't use any of these:

我有一个锚标记的 href 值,它只有 href 值作为属性。现在我想在页面中找到与我的 href 值具有相同值的元素并单击它。我无法找到任何使用标准硒方法执行此操作的方法。我该怎么做?基本上这些是我找到的功能,但似乎我不能使用其中任何一个:

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

采纳答案by Dmytro Pastovenskyi

You can use find_element_by_xpath functionality.

您可以使用 find_element_by_xpath 功能。

driver.find_element_by_xpath('//a[@href="'+url+'"]')

回答by rnevius

You would find the element by the CSS selector, as you would using vanilla CSS:

您可以通过 CSS 选择器找到该元素,就像使用 vanilla CSS 一样:

link = driver.find_element_by_css_selector('[href^=http://somelink.com/]')

You can also find the element by the link text:

您还可以通过链接文本找到该元素

link = driver.find_element_by_partial_link_text('somelink')

回答by Netra Shah

You can try this:

你可以试试这个:

driver.find_element_by_xpath('//a[contains(@href,"href")]')