0

I have a case like this: I crawl one website but the website have a popup show random when the website show a popup I need it to wait 180 seconds. I want to check the condition popup and click to button for turn-off popup.

I want to ask: How to check the condition and click the button in popup with Selenium and Python but not have slow. I was can do that with my code but the time to find_element was too slow, it loses about 15-30 seconds for delay. This is my code:

The popup is random soo if when it does not show the popup. It will lose a lot of time in row find_element.

This is first code:

try:
    print('Try find Alert box')
    checkup_click_understand = driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/div/div[3]/button')
    if checkup_click_understand.is_displayed():
        actions = webdriver.ActionChains(driver)
        actions.click(checkup_click_understand)
        actions.perform()
        time.sleep(180)
except NoSuchElementException:
    print('No Alert BOX')

I tried another way but it has the same problem. This is the second code:

try:
    print('Try find Alert box')
    # checkup_click_understand = WebDriverWait(driver, 3).until(
    #     EC.presence_of_element_located((By.XPATH, "/html/body/div[4]/div[3]/div/div[3]/button"))
    # )
    wait = WebDriverWait(driver, 1)
    checkup_click_understand = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[3]/div/div[3]/button')))
    print(checkup_click_understand.is_displayed())
    if checkup_click_understand.is_displayed():
        actions = webdriver.ActionChains(driver)
        actions.click(checkup_click_understand)
        actions.perform()
        time.sleep(180)
except:
    print('No Alert BOX')

And the third code. I try to get the size to condition, but it same problem, take a lot of time when the code run to checkup_click_understand = WebDriverWait(driver, 3). I was think the code will work and I only need 3 seconds to wait, but it not, I need to spend 15-30s like the first and the second way.

try:
    print('Try find Alert box')
    checkup_click_understand = WebDriverWait(driver, 3).until(
        EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[3]/div/div[3]/button'))
    )
    if checkup_click_understand.size > 0:
        actions = webdriver.ActionChains(driver)
        actions.click(checkup_click_understand)
        actions.perform()
        time.sleep(180)
except TimeoutException:
    print('No Alert BOX')

1 Answer 1

1

WebDriverWait does not wait FOR 3 seconds, it waits UP TO 3 seconds. If the item appears before that it will click on it immediately.

for example: your popup appears after 15 seconds, but your wait is 20, your script will still click after 15 seconds; as soon as the popup appears.

with regard to clicking on the popup; item.click() doesn't work for you?

try:
    print('Try find Alert box')
    checkup_click_understand = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[3]/div/div[3]/button')))
    checkup_click_understand.click()
    # if checkup_click_understand.size > 0:
    #     actions = webdriver.ActionChains(driver)
    #     actions.click(checkup_click_understand)
    #     actions.perform()
    time.sleep(180)
except TimeoutException:
    print('No Alert BOX')

if not, this is how I would use action chains for simple click().

try:
    print('Try find Alert box')
    checkup_click_understand = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[3]/div/div[3]/button')))
    #checkup_click_understand.click()
    if checkup_click_understand.size > 0:
        actions = webdriver.ActionChains(driver)
        actions.move_to_element(checkup_click_understand).click().perform()
    time.sleep(180)
except TimeoutException:
    print('No Alert BOX')
Sign up to request clarification or add additional context in comments.

4 Comments

Can you share with me a document about that? Why do you not use actions.click? I was thinking it same. I will test with your code share with me, thanks you so much!!!
I tried with your code. But it same with mine, it lost 15-30s in the row checkup_click_understand = WebDriverWait(driver, 3).until(EC.ele.... Have some time for the reason DOM of the website?
You may need to send more of your code; the portion provided should work quickly if your xpath is provided; and at most 3-4 seconds if the xpath is not correct (or clickable). Have you tried: EC.presence_of_element_located? Also, actionchains are used for sequences or combinations of inputs; click+drag, click in a certain spot; etc. check out more here: selenium.dev/selenium/docs/api/py/webdriver/…
Yes! Thank you so much for supporting me! I will try again. I found the reason for this slow problem. The website I need automation has a slow server and DOM too big for selenium run and check, I am not sure 100% but I try with your code on a different page it works quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.