Find and click element based on @onclick w/ Selenium

可紊 提交于 2019-12-08 10:00:47

问题


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.


回答1:


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"]')


来源:https://stackoverflow.com/questions/52191015/find-and-click-element-based-on-onclick-w-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!