我试图点击一个有以下HTML代码的盒子。
<li>
<input id="mce-group[166]-166-0" type="checkbox" value="1" name="group[166][1] >
<label for="mce-group[166]-166-0">I agree</label>
</li>
我都试过了: id,name,xpath,text,... ...运行像这样的东西。
select_box = driver.find_element_by_xpath('//*[@id="mce-group[166]-166-0"]')
select_box.click()
我得到了这个错误。
selenium.common.exceptions.ElementNotInteractableException: Message: Element <input id="mce-group[166]-166-0" name="group[166][1]" type="checkbox"> could not be scrolled into view
要点击 <input>
与文本相关的元素 我同意 你需要诱导 WebDriverWait 对于 element_to_be_clickable()
您可以使用以下任何一种方式 定位策略:
使用 CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for=\"mce-group[166]-166-0\"]"))).click()
使用 XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for=\"mce-group[166]-166-0\"]"))).click()
说明: : 你必须添加以下进口。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC