简体   繁体   English

Select 输入元素按 id 使用 Selenium Python

[英]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.

相关问题 Selenium Python select 输入元件 - Selenium Python select input element 使用Selenium选择一个输入元素 - Select an input element using Selenium 如何使用Python Selenium选择输入元素 - How to select input element with Python Selenium 尝试通过 selenium python 输入框 select 但每次我重新加载页面时元素中的“id”都会更改 - Trying to select an input box through selenium python but the 'id' in the element changes every time I reload the page 如何选择具有该名称的Microsoft CRM表单上的Last Name元素 <input id = “lastname_i”…> 使用Python selenium驱动程序? - How do I select the Last Name element on Microsoft CRM form that has the name <input id = “lastname_i”…> using Python selenium driver? 使用 Selenium 和 Python 定位具有动态 ID 的元素 - Locating an element with dynamic ID using Selenium and Python 使用Selenium和python按部分ID选择元素? - Choose element by partial ID using Selenium with python? 在Python测试中使用Selenium通过ID获取元素 - Using selenium to get element by id in python testing 无法在 selenium 和 Z23EEEB4347BDD26BFC6B7EE9A3B75 中使用 xpath 的元素 select - unable to select an element using xpath in selenium and python 使用 Python selenium 选择一个元素(文本) - Select an element (text) using Python selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM