在 Selenium 中有 4 种点击方式。
我将使用这个 xpath
//span[contains(text(), 'about us')]//parent::a[@id='about_page']
代码试用 1:
time.sleep(5)
driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']").click()
代码试用 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'about us')]//parent::a[@id='about_page']"))).click()
代码试用 3:
time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']")
driver.execute_script("arguments[0].click();", button)
代码试用 4:
time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']")
ActionChains(driver).move_to_element(button).click().perform()
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains