Python 等待,然后单击某个项目?
因此,我想等待弹出窗口,然后单击“接受”。 HTML如下所示:Python 等待,然后单击某个项目?,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,因此,我想等待弹出窗口,然后单击“接受”。 HTML如下所示: <div class="termsBtn" onclick="checkTerms(0)" style="background-color:#dd4a42">Decline</div> <div class="termsBtn" onclick="checkTerms(1)" style="background-color:#a6dd42">Accept</div> 我的当前代码:
<div class="termsBtn" onclick="checkTerms(0)" style="background-color:#dd4a42">Decline</div>
<div class="termsBtn" onclick="checkTerms(1)" style="background-color:#a6dd42">Accept</div>
我的当前代码:
from selenium.webdriver.support import expected_conditions as ec
wait = WebDriverWait(driver, 10)
popup = wait.until(ec.element_to_be_clickable((By.XPATH, '//input[@onclick="checkTerms(1)"]')))
popup.click()
元素没有输入标记,它是一个div标记。请尝试下面的xpath
wait = WebDriverWait(driver, 10)
popup = wait.until(ec.element_to_be_clickable((By.XPATH, '//div[@class="termsBtn"][text()="Accept"]')))
popup.click()
您可以使用ExpectedConditions.visibilityOfElementLocated,它将等待元素不可见 资料来源:
由于所需元素位于弹出窗口中,因此要在元素上单击(),您必须引导WebDriverWait使
元素可单击()
,并且您可以使用以下任一解决方案:
- 使用
:CSS\u选择器
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.termsBtn[onclick*='1']"))).click()
- 使用
:XPATH
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='termsBtn' and text()='Accept']"))).click()
- 注意:您必须添加以下导入:
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.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC