[英]Find and click element based on @onclick w/ Selenium
I am trying to find a element using Selenium in Python that has the html like this: 我试图在Python中使用Selenium来找到一个具有如下html元素:
<td class="td1" onclick="window.location.href = 'work_area.php?SID';">Work area</td>
Contained in a simple <tr></tr>
tag. 包含在简单的
<tr></tr>
标记中。
I have tried using 我尝试使用
work_area = driver.find_element_by_css_selector('td[contains(@onclick, "work_area.php?SID"]')
and using an absolute xpath 并使用绝对xpath
work_area = driver.find_element_by_xpath("/html/body/table/tbody/tr[2]/td[1]/div/table/tbody/tr[2]/td/table/tbody/tr[3]/td")
My buttons structure: 我的按钮结构:
+---------------------+
| Menu |
+---------------------+
| Work Area |
+---------------------+
| Details |
+---------------------+
I am using Python 3.6, and Selenium is up to date, any suggestions? 我正在使用Python 3.6,并且Selenium是最新的,有什么建议吗? I have also tried different suggestions that I could find on stackoverflow but nothing helped.
我还尝试了一些关于stackoverflow的建议,但没有任何帮助。
You can use locators below: 您可以在下面使用定位器:
driver.find_element_by_css_selector("td[onclick*='work_area']")
driver.find_element_by_xpath("//td[.='Work area']")
You code below wasn't correct, locator is xpath and you use css selector: 您下面的代码不正确,定位器是xpath,并且使用了CSS选择器:
work_area = driver.find_element_by_css_selector('td[contains(@onclick,"work_area.php?SID"]')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.