简体   繁体   English

基于@onclick w / Selenium查找并单击元素

[英]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.

相关问题 如何自动化 Selenium 以单击图像按钮 - 通过 onclick 查找元素? - Python - How to automate Selenium to click image button - find element by onclick? - Python 硒查找元素,然后单击它 - Selenium find element and click on it Selenium Chromedriver:当禁用 w3c 时,&#39;dict&#39; 对象没有 find_element() 方法的属性 &#39;click&#39; - Selenium Chromedriver : 'dict' object has no attribute 'click' for find_element() method when w3c is disabled Selenium Python:单击包含 Z686155AF75A60A0F36E9D80C1FEZEDD 的 onclick 元素 - Selenium Python: click on onclick element containing JavaScript (Python + selenium) 如何点击“onclick”元素? - (Python + selenium) how to click on "onclick" element? 如何使用 Selenium 和 Python 单击具有 onclick 属性的基于 javascript/AJAX 的元素 - How to click on a javascript/AJAX based element with onclick attribute using Selenium and Python 如何使用Selenium和Python通过“onclick”查找并单击按钮? - How to find and click the button by "onclick" with Selenium and Python? Python Selenium:查找并单击一个元素 - Python Selenium: find and click an element 查找并单击元素... Python Selenium - Find and click element by… Python Selenium 如何在 selenium 中找到并单击元素? - How to find and click element in selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM