0

H, I am trying to click on the below checkbox.

<thead class="ant-table-thead" xpath="1">
  <tr>
    <th class="ant-table-selection-column"><span class="ant-table-header-column"><div><span class="ant-table-column-title"><div class="ant-table-selection"><label class="ant-checkbox-wrapper"><span class="ant-checkbox"><input type="checkbox" class="ant-checkbox-input" value=""><span class="ant-checkbox-inner"></span></span>
      </label>
      </div>
      </span><span class="ant-table-column-sorter"></span></div>
      </span>
    </th>

 
  </tr>
</thead>

It gets clicked instantly only as follows:

Checkbox = "//thead/tr[1]/th[1]/span[1]/div[1]/span[1]/div[1]/label[1]/span[1]/input[1]"
driver.find_element_by_xpath(Checkbox).click()

However, when I try to add explicit wait, the checkbox doesn't get clicked

if WebDriverWait(driver, 30).until(
                    EC.element_to_be_clickable((By.XPATH, Checkbox))): driver.find_element_by_xpath(
                        Checkbox).click()

I have also tried EC.visibility_of_element_located but also doesn't work

Thanks for the help in advance

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ryan
  • 129
  • 1
  • 12

2 Answers2

0

looking at your code and at the documentation i think i figured out, i cannot reproduce to test but i will try to explain why isn't working:

From the doc:

from selenium.common.exceptions import TimeoutException
    try:
        element = WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH, Checkbox))

        driver.find_element_by_xpath(Checkbox).click()
    except TimeoutException as ex :
        
        *put here the "else" code, it will be execute if the wait ends with no Checkbox found so the WebDriver will Throw a TimeoutException*
gianoseduto
  • 188
  • 1
  • 11
0

To click on the 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, "thead.ant-table-thead input.ant-checkbox-input"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//thead[@class='ant-table-thead']//input[@class='ant-checkbox-input']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Im already doing that, but the element just doesn't get clicked when using webdriver wait – Ryan Jan 24 '21 at 20:32