![](/img/trans.png)
[英]Selenium Python: click on onclick element containing JavaScript
[英]How to click on a javascript/AJAX based element with onclick attribute using Selenium and Python
我正在尝试单击 Selenium 下拉菜单中的链接。
我像这样访问元素:
link = menu.find_element_by_xpath('//*[contains(text(), "Mark as shipped")]')
链接的 href 是javascript.void(0)
,并且包含一个onclick
属性,其中包含:
'com.ebay.app.myebay2.lineaction.service.LineActionAjax.processTransRequest("http://payments.ebay.com/ws/eBayISAPI.dll?OrderAction&transId=#TID#&action=4&pagetype=1883&ssPageName=STRK:MESO:SHP&itemid=_Item_Id", "_Item_Id", "987349587", "MarkShipped", "98739873", "_Item_Id_9874987_ss", 24")'
我试过用以下方法触发:
click()
和
driver.execute_script(link.get_attribute('onclick'))
还有一个 ActionChain 鼠标悬停在链接上并单击它。
但似乎没有一个工作。 我如何触发这个?
该元素是一个AJAX元素,因此理想情况下,单击需要为element_to_be_clickable()
引入WebDriverWait的元素,您可以使用以下任一定位器策略:
使用PARTIAL_LINK_TEXT
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Mark as shipped"))).click()
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick*='MarkShipped']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@onclick, 'MarkShipped') and contains(., 'Mark as shipped')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.