0

When you load a page with a student, at least two options appear for you to click (if they studied high school, in the same house of studies). But this is not always the case. I want to condition the following:

wait = WebDriverWait(driver, 2)

if not wait.until(EC.element_to_be_clickable((By.XPATH, "//td[text()='AC']/following-sibling::td/input"))):
    print('Cargando reporte...')
    time.sleep(6)
    driver.save_screenshot(r'C:\Users\spvda\Desktop\{}.-image.png'.format(i))
    driver.quit()   
else:
    ait.until(EC.element_to_be_clickable((By.XPATH, "//td[text()='AC']/following-sibling::td/input"))).click()
    print('Cargando reporte...')
    time.sleep(6)
    driver.save_screenshot(r'C:\Users\spvda\Desktop\{}.-image.png'.format(i))
    driver.quit()

But, it don't work and get this: raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: (The message is empty).

How fix this?

1
  • 1
    Try/Except would be the "Best practice" in that case. It makes you handle different errors as you like Commented Aug 2, 2021 at 14:12

1 Answer 1

2

You can use find_elements to have a list and can check if the size > 0 then do some stuff othetrwise do something else.

try:
    if(len(driver.find_elements(By.XPATH, "//td[text()='AC']/following-sibling::td/input"))) > 0:
        print("When AC is visible in UI")
        wait.until(EC.element_to_be_clickable((By.XPATH, "//td[text()='AC']/following-sibling::td/input"))).click()
        print('Cargando reporte...')
        time.sleep(6)
        driver.save_screenshot(r'C:\Users\spvda\Desktop\{}.-image.png'.format(i))
        driver.quit()
    else:
        print('Cargando reporte...')
        time.sleep(6)
        driver.save_screenshot(r'C:\Users\spvda\Desktop\{}.-image.png'.format(i))
        driver.quit()
except:
    print("Something went wrong")
    pass
Sign up to request clarification or add additional context in comments.

3 Comments

The first conditional detects the button, but does not click the button.
cause the code isn't there to click, updated above, please try again
Forget it, I added in my code. Your code is correct. Thanks

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.