![](/img/trans.png)
[英]How to automate Selenium to click image button - find element by onclick? - Python
[英]Find and click element based on @onclick w/ Selenium
我試圖在Python中使用Selenium來找到一個具有如下html元素:
<td class="td1" onclick="window.location.href = 'work_area.php?SID';">Work area</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")
我的按鈕結構:
+---------------------+
| Menu |
+---------------------+
| Work Area |
+---------------------+
| Details |
+---------------------+
我正在使用Python 3.6,並且Selenium是最新的,有什么建議嗎? 我還嘗試了一些關於stackoverflow的建議,但沒有任何幫助。
您可以在下面使用定位器:
driver.find_element_by_css_selector("td[onclick*='work_area']")
driver.find_element_by_xpath("//td[.='Work area']")
您下面的代碼不正確,定位器是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.