[英]Select input element by id using Selenium Python
我正在嘗試單擊具有以下 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>
元素,我同意您需要為element_to_be_clickable()
誘導WebDriverWait並且您可以使用以下任一定位器策略:
使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.