简体   繁体   中英

I can't do check box by using Python Selenium

I tried to do check box by using Python Selenium but I got the following error.

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

The element is wrong but I'm not sure where it is. This is how HTML is.

<div _ngcontent-yfw-c1="" class="col-md-12 text-center form-group mt-4 p-2" id="iAcceptCheckboxLabel">
 <input _ngcontent-yfw-c1="" class="checkbox ng-valid ng-dirty ng-touched" id="iAcceptCheckbox" name="checkbox"      type="checkbox">
 <label _ngcontent-yfw-c1="" class="font-weight-normal" for="iAcceptCheckbox"> I accept the 
  <a _ngcontent-yfw-c1="" class="ng-tns-c1-0" href="/terms" id="terms-link" target="_blank">terms and conditions</a>          
 </label>
</div>

I did python like this.

# I accept the terms and conditions
check = driver.find_element_by_class_name("checkbox")
check.click()
time.sleep(1)

在此处输入图像描述

Added

Finally, this code succeeded.

check = driver.find_element_by_id("iAcceptCheckbox")
driver.execute_script("arguments[0].click();",check)

Try this

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_id("iAcceptCheckbox")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
driver.execute_script("arguments[0].click();", element)

Update:

Try such approach

from selenium import webdriver
from selenium.webdriver.common.by import By

def scroll_element_into_view(driver, element):
    y = element.location['y']
    driver.execute_script('window.scrollTo(0, {0})'.format(y))
    
element = driver.find_element(By.XPATH, "//div[@id='iAcceptCheckboxLabel']")
scroll_element_into_view(driver, element)
element.click()

The element maybe inside a frame, so you have to switch to that frame first to be able to deal with the element driver.switch_to.frame

Click the label for the input tag.

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='iAcceptCheckbox']"))).click() 

Imports

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

Please click the input tag.

Try the following selector and use ActionChains :

check = driver.find_element_by_css_selector('div#iAcceptCheckboxLabel input#iAcceptCheckbox')
#edited here
driver.execute("arguments[0].scrollIntoView();", check)
action = ActionChains(driver)
action.move_to_element(check).click(check).perform()

Following import:

from selenium.webdriver import ActionChains

Click the div element for label

As the div element is displayed in foreground. You cannot click the input directly as the click will be intercepted by this div tag

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='iAcceptCheckboxLabel']"))).click() 

Imports

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

Update

check= WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='iAcceptCheckboxLabel']")))
action = webdriver.ActionChains(driver)
action.move_to_element_with_offset(check,1,2).click().perform()

Checking a check box in Selenium should be easy, I think you just have a small syntax error

check = driver.find_element_by_class_name("checkbox")
check.click()
time.sleep(1)

Try the following...

check = driver.find_element_by_name("checkbox")
check.click()
time.sleep(1)

I think the problem is just that you had "driver.find_element_by_class_name

But you specified the name attribute. Not the class attribute

from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 

check = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "iAcceptCheckbox")) 

When ever you can, try to use by_id because it is the fastest, then name, and then xpath or css. For the not interactble element use explicit wait.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM