基于 @onclick w/Selenium 查找并单击元素 [英] Find and click element based on @onclick w/ Selenium
问题描述
我正在尝试在 Python 中使用 Selenium 查找具有如下 html 的元素:
<td class="td1" onclick="window.location.href = 'work_area.php?SID';">工作区</td>
包含在一个简单的 <tr></tr>
标签中.
我试过使用
work_area = driver.find_element_by_css_selector('td[contains(@onclick, "work_area.php?SID"]')
并使用绝对 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")
我的按钮结构:
+---------------------+|菜单 |+---------------------+|工作区 |+---------------------+|详情 |+---------------------+
我使用的是 Python 3.6,Selenium 是最新的,有什么建议吗?我还尝试了可以在 stackoverflow 上找到的不同建议,但没有任何帮助.
您可以使用以下定位器:
driver.find_element_by_css_selector("td[onclick*='work_area']")driver.find_element_by_xpath("//td[.='工作区']")
您下面的代码不正确,定位器是 xpath 并且您使用了 css 选择器:
<块引用>work_area = driver.find_element_by_css_selector('td[contains(@onclick,"work_area.php?SID"]')
I am trying to find a element using Selenium in Python that has the html like this:
<td class="td1" onclick="window.location.href = 'work_area.php?SID';">Work area</td>
Contained in a simple <tr></tr>
tag.
I have tried using
work_area = driver.find_element_by_css_selector('td[contains(@onclick, "work_area.php?SID"]')
and using an absolute 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? I have also tried different suggestions that I could find on stackoverflow but nothing helped.
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:
work_area = driver.find_element_by_css_selector('td[contains(@onclick,"work_area.php?SID"]')
这篇关于基于 @onclick w/Selenium 查找并单击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!