How to click on checkbox with selenium in python
It’s likely a timing issue. Many pages dynamically build DOM structure after they load.
Try adding a sleep for a second or two after loading the URL before trying to find that element. For the most robustness, you would put that in a loop and timeout after 10 seconds or so.
First, check answer above about timing issue.Second, I think that you are trying to find a dynamic id locator.Try to find by css.
browser.find_element_by_css_selector('.deliveryCheckbox.hiddenCheckbox').click()
Also, try using the xpath in the case when label
is what you really need to click:
browser.find_element_by_xpath("//label[contains(@for,'deliveryCheckbox')]")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,"//label[@for='deliveryCheckbox-684']"))).click()
You'd have induce waits for the element to be clickable and click the label tag.
Import
from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC