Search code examples
pythonselenium-webdriverfindelement

How to click a Checkbox by driver.find_element(id in Python


I am trying to automate my register/login work.

My problem is that I cannot "click" the Checkbox to accept the "Terms of use"

HTML Code of the Website:

<div class="formList-content"><label class="checkbox checkbox--brand ">
<input class="input checkbox-input" name="termsConditionsAndPrivacy" 

id="registerForm-termsConditionsAndPrivacy" data-handler="focus-toggle" 

data-focus-toggle="{&quot;toggles&quot;:[{&quot;className&quot;:&quot;input-wrapper--focus&quot;,&quot;target&quot;:&quot;.formList-row/.input-wrapper&quot;}]}" 

type="checkbox"><span class="tGrid-cell tGrid-cell--shrink">

<span class="checkbox-box flex--inline boxAlign-jc--all-c boxAlign-ai--all-c">

<svg width="18px" height="14px" class="icon icon--tick text--color-brandPrimary checkbox-tick">

<use xlink:href="/assets/img/ico_ca8e3.svg#tick"></use></svg></span></span>

<span class="checkbox-text tGrid-cell space--l-2 space--t-0 vAlign--all-m mute--text">Ich habe die AGB <a class="link" href="/regeln-und-bedingungen" rel="nofollow" target="_blank">Regeln und Bedingungen

</a> sowie die <a class="link" href="/datenschutz" rel="nofollow" target="_blank">Datenschutzerklärung</a> gelesen und akzeptiert.</span></label></div>

The code I tried ↓

driver.find_element(id, "registerForm-termsConditionsAndPrivacy").click()

got me this error:

Traceback (most recent call last):
  File "C:\Users\Admin\PycharmProjects\Bot 2\main.py", line 17, in <module>
    driver.find_element(id, "registerForm-termsConditionsAndPrivacy").click()

  File "C:\Users\Admin\PycharmProjects\Bot 2\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 740, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]

  File "C:\Users\Admin\PycharmProjects\Bot 2\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 344, in execute
    response = self.command_executor.execute(driver_command, params)

  File "C:\Users\Admin\PycharmProjects\Bot 2\venv\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 288, in execute
    data = utils.dump_json(params)

  File "C:\Users\Admin\PycharmProjects\Bot 2\venv\lib\site-packages\selenium\webdriver\remote\utils.py", line 24, in dump_json
    return json.dumps(json_struct)

  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)

  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)

  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)

  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type builtin_function_or_method is not JSON serializable

Process finished with exit code 1

How do I make it click the TOS Checkbox ?

Tried using "driver.find_element(id, "registerForm-termsConditionsAndPrivacy").click()"


Solution

  • Here's how you may try:

    import time
    from selenium.webdriver import Chrome
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    import selenium.webdriver.support.expected_conditions as EC
    
    driver = Chrome()
    driver.get("https://www.mydealz.de/register")
    wait = WebDriverWait(driver, 10)
    
    # accept all cookies
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'button[data-t="acceptAllBtn"]'))).click()
    
    checkboxes = driver.find_elements(By.CSS_SELECTOR, 'span.tGrid-cell.tGrid-cell--shrink')
    # select the 2nd checkbox
    checkboxes[1].click()
    
    time.sleep(2)
    

    checkbox screenshot

    Similarly, you can also select the 1st checkbox using checkboxes[0].click()

    I hope this solves your problem.