![](/img/trans.png)
[英]How to click on second element from list of web element in python selenium?
[英]How to click the second element if the first element is not clickable using Python Selenium
如果使用 Python Selenium 無法單擊第一個元素,如何單擊第二個元素
代碼試驗:
if self.driver.find_element(By.CSS_SELECTOR, ".estimator-container:nth-child(3) .btn").is_not_clicked():
self.driver.find_element(By.LINK_TEXT, "Dont have the Plate?").click();
要點擊任何可點擊的元素,您需要誘導WebDriverWait讓element_to_be_clickable()
將代碼塊包裝在try-except{}
塊中,您可以使用以下定位器策略:
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".estimator-container:nth-child(3) .btn"))).click()
print("First element was clicked")
except TimeoutException:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Dont have the Plate?"))).click()
print("Second element was clicked")
注意:您必須添加以下導入:
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.