Python, Selenium: 如何点击 javascript:void(0) 元素
Python, Selenium: how to click on javascript:void(0) element
我在网页中有一个“onclick”元素,
<a href="javascript:void(0)" onclick=
"toIndexHtml('3','http://xxxxxxxxxxx/trade','0')">
<i></i>
<span></span>
trade
</a>
它在网页中显示为一个按钮,我想点击它,我尝试使用以下代码搜索它:
driver.find_element_by_xpath("//a[contains(@onclick,'toIndexHtml')]").click()
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,"/html/body/div/ul/li[3]/a"))).click()
两个都不行,有没有其他方法请指教!提前致谢!
P.S.: 我正在使用 Chrome WebDriver 和 Chrome v64.
您的第一个定位器看起来很完美,应该可以使用。
理想情况下,点击你需要诱导的元素 for the element_to_be_clickable()
and you can use either of the following :
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='toIndexHtml']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick,'toIndexHtml')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我在网页中有一个“onclick”元素,
<a href="javascript:void(0)" onclick=
"toIndexHtml('3','http://xxxxxxxxxxx/trade','0')">
<i></i>
<span></span>
trade
</a>
它在网页中显示为一个按钮,我想点击它,我尝试使用以下代码搜索它:
driver.find_element_by_xpath("//a[contains(@onclick,'toIndexHtml')]").click()
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,"/html/body/div/ul/li[3]/a"))).click()
两个都不行,有没有其他方法请指教!提前致谢!
P.S.: 我正在使用 Chrome WebDriver 和 Chrome v64.
您的第一个定位器看起来很完美,应该可以使用。
理想情况下,点击你需要诱导的元素element_to_be_clickable()
and you can use either of the following
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='toIndexHtml']"))).click()
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick,'toIndexHtml')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC