maurice dv
maurice dv

Reputation: 3

Wait for and then click an item?

So I would like to wait for a pop-up and then click Accept. HTML looks like this:

<div class="termsBtn" onclick="checkTerms(0)" style="background-color:#dd4a42">Decline</div>

<div class="termsBtn" onclick="checkTerms(1)" style="background-color:#a6dd42">Accept</div>


I have tried all sorts, but for this current code I am getting a TimeoutException:

selenium.common.exceptions.TimeoutException: Message: 



My current code:

from selenium.webdriver.support import expected_conditions as ec


wait = WebDriverWait(driver, 10)
popup = wait.until(ec.element_to_be_clickable((By.XPATH, '//input[@onclick="checkTerms(1)"]')))
popup.click()

Upvotes: 0

Views: 713

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193108

As the desired element is within a popup so to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.termsBtn[onclick*='1']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='termsBtn' and text()='Accept']"))).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
    

Upvotes: 0

Shubham Jain
Shubham Jain

Reputation: 17553

You can use ExpectedConditions.visibilityOfElementLocated, it will wait till the element not visible

Source:

Webdriver How to wait until the element is clickable in webdriver C#

Upvotes: 0

KunduK
KunduK

Reputation: 33384

There is no input tag of your element it is a div tag.Try below xpath.

wait = WebDriverWait(driver, 10)
popup = wait.until(ec.element_to_be_clickable((By.XPATH, '//div[@class="termsBtn"][text()="Accept"]')))
popup.click()

Upvotes: 1

Related Questions