给出 html:
<div class="disclaimer-buttons-place-holder">
<a class="btn btn-reverse" href="WEBSITE" data-event-zone="click" data-event="C" data-event-label="action::disclaimer::exit" data-event-type="A"> Refuser</a>
<a class="btn" id="validateDisclaimer" href="/fr_part" data-event-zone="click" data-event="C" data-event-label="action::disclaimer::validate" data-event-type="A"> ccepter et continuer</a>
</div>
所需元素是动态元素。
要单击 clickable 元素,您需要为 element_to_be_clickable() 引入 WebDriverWait,并且您可以使用以下任一定位器策略:
使用PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "ccepter et continuer"))).click()
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn#validateDisclaimer[data-event-label$='validate']"))).click()
使用XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn' and @id='validateDisclaimer'][contains(@data-event-label, 'validate') and contains(., 'ccepter et continuer')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC