0

I'm trying to select all checkboxes from a consent page like so:

enter image description here

The element I'm trying to select is:

    <div class="tableCell rowBody headTitle">
        <span class="deskOnly">Email</span>
        <div class="ckeckAll">
            <input type="checkbox" id="allEmail" for="Email" class="allNone bigCheck cbChecked"
                value="true"><label for="allEmail" class="consentCheck"><span></span></label>
            <label for="allEmail" class="mobileOnly">Email</label>
            <div class="downArrow deskOnly" for="allEmail"></div>
            <ul id="menuEmail" for="allEmail"
                class="dropMenu ui-menu hoverMenu deskOnly ui-widget ui-widget-content" role="menu"
                tabindex="0" style="display: none;" aria-activedescendant="ui-id-1">
                <li class="All ui-menu-item">
                    <div id="ui-id-1" tabindex="-1" role="menuitem"
                        class="ui-menu-item-wrapper menuText">All</div>
                </li>
                <li class="None ui-menu-item">
                    <div id="ui-id-2" tabindex="-1" role="menuitem"
                        class="ui-menu-item-wrapper menuText">None</div>
                </li>
            </ul>
        </div>
    </div>

I tried using a Select but got an error to say you can only use this with elements, not elements.

Any help would be appreciated. Thanks.

crazzyfool
  • 13
  • 3

2 Answers2

0

First try this:

allCheckbox = driver.find_element(By.XPATH, "//input[@id='allEmail']")
allCheckbox.click()

If that doesn't work, use JS to perform click() as below:

allCheckbox = driver.find_element(By.XPATH, "//input[@id='allEmail']")
driver.execute_script("arguments[0].click();", allCheckbox)
Shawn
  • 4,064
  • 2
  • 11
  • 23
0

The desired element is an and an <input> element so using Select() won't work.


To click on on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#allEmail"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='allEmail']"))).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
    

Incase the <label> overlaps the <input> element you can use either of the following (Of the many findElement(s)/By functions in Selenium, when would you use one over the other?):

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='allEmail']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='allEmail']"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352