Python + Selenium:如何点击“ onclick”元素? [英] Python + Selenium: How can click on "onclick" elements?
问题描述
我的网页中有一个 onclick元素,其HTML内容如下:
I have an "onclick" element in a webpage whose HTML reads as follows:
<a href="#" onclick="fastener('3625')">Fastener</a>
我想使用字符串 fastener或 Fastener使用Python + Selenium搜索此元素。数字 3625将根据以前的输入而变化,因此无法搜索。
I want to search this element using the string "fastener" or "Fastener" using Python + Selenium. The number "3625" will change depending on previous inputs, and hence cannot be searched for.
我尝试了以下操作,但徒劳地进行了操作:
I tried the following, but in vain:
br.find_element_by_css_selector("a[@onlick*='fastener']").click()
请建议执行此操作的方法。谢谢!
Please suggest ways to do this. Thank you!
PS:我正在使用Python 2.7,以及Chrome WebDriver和Chrome v62。
P.S.: I am using Python 2.7, with Chrome WebDriver and Chrome v62.
推荐答案
要搜索文本为 Fastener 的元素,可以使用以下任一选项:
To search the element with text as Fastener you can use either of the following options :
-
通过紧固件:
br.find_element_by_link_text("Fastener").click()
通过紧固件使用 onclick (xpath):
Using onclick through fastener (xpath):
br.find_element_by_xpath("//a[contains(@onclick,'fastener')]").click()
使用 onclick 通过紧固件(css_selector):
Using onclick through fastener (css_selector):
br.find_element_by_css_selector("a[onclick^='fastener']").click()
这篇关于Python + Selenium:如何点击“ onclick”元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!