Hebrides
Hebrides

Reputation: 21

How to check checkbox using Selenium in Python

I have a problem with this checkbox. I tried to click searching element with id, name, XPath, CSS Selector and contains text and still I could not click on this checkbox. Additionally, I've tried with another site with similar HTML code and on this site, it was enough to look for id and click. Any ideas?

<div class="agree-box-term">
    <input tabindex="75" id="agree" name="agree" type="checkbox" value="1">
    <label for="agree" class="checkbox-special">* Zapoznałam/em się z <a href="https://worldbox.pl/content/regulamin,27.html" target="_blank">Regulaminem sklepu internetowego</a> i akceptuję jego postanowienia.<br></label>
</div>

Here is my Python code https://codeshare.io/5zo0Jj

Upvotes: 1

Views: 522

Answers (4)

JimmyA
JimmyA

Reputation: 686

I don't know why this is, but in my experience some boxes don't accept click but do accept a 'mousedown' trigger.

try:

driver.execute_script('$("div.agree-box-term input#agree").trigger("mousedown")')

This solution does rely on jquery being on the page, if it's not we can write it in javascript

Upvotes: 1

KunduK
KunduK

Reputation: 33384

I have used javaScript Executor and it clicks on the element.However I have also checked webdriver click is not working.

driver.execute_script("arguments[0].click();", driver.find_element_by_id("agree"))

Upvotes: 1

mliakos
mliakos

Reputation: 383

Does your code contain nested html tags? For example:

<html>

    <div>

        <p> Some text </p>

        <html>
            That block can't be traversed!
        </html>

    </div>

</html>

Anything inside the second HTML tags can't be traversed/accessed. Try to see if that's the case.


In any other case the following code ran perfectly fine for your snippet:

driver.find_element_by_css_selector('#agree').click()

Upvotes: 0

Philippe Roubert
Philippe Roubert

Reputation: 46

r = driver.find_element_by_xpath("//*[@id="form-order"]/div[2]/div[4]/label")
r.click()

Does this work for you? Sometimes it's just a question of selecting the right xpath, or adding the brackets after click.

Upvotes: 0

Related Questions