[英]Select input element by id using Selenium Python
I'm trying to click on a box that has the following HTML code:我正在尝试单击具有以下 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>
I've tried it all: id, name, xpath, text,... Running something like this:我已经尝试了所有方法:id,name,xpath,text,......运行这样的东西:
select_box = driver.find_element_by_xpath('//*[@id="mce-group[166]-166-0"]')
select_box.click()
I get this error:我收到此错误:
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
To click on the <input>
element associated with the text I agree you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies :要单击与文本关联的
<input>
元素,我同意您需要为element_to_be_clickable()
诱导WebDriverWait并且您可以使用以下任一定位器策略:
Using CSS_SELECTOR
:使用
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for=\"mce-group[166]-166-0\"]"))).click()
Using XPATH
:使用
XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for=\"mce-group[166]-166-0\"]"))).click()
Note : You have to add the following imports:注意:您必须添加以下导入:
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.