Home > Software engineering >  How to click on a pop-up button with Selenium using Python
How to click on a pop-up button with Selenium using Python

Time:08-31

I want to click on a popup window but after trying several ways, I obtain a "TimeoutException" error.

This is the code that I'm trying:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='modal-footer modal-footer-gallit-postula' and @id='modal_comentarios']"))).click()

And I attach an image of the htmlenter image description here

For further information, the popup is very similar to the one appearing in the url below after clicking on the green button that says "Postular" (to get the exact popup it's necessary to be logged in). https://trabajo.gallito.com.uy/anuncio/vendedor-automotriz-qm995

CodePudding user response:

The desired element is within a Modal Dialog Box so to click 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, "div.modal-footer.modal-footer-gallit-postula > button.btn.btn-primary.btn-color-postula"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-primary btn-color-postula' and text()='Aceptar']"))).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
    

CodePudding user response:

You can use time.sleep() to handle this. For context:

    from selenium import...
    import time

    driver.get(url)
    butn = driver.find_element(by=By.XPATH, value='//* 
    [@id="postular"]').click() 
    time.sleep(5)
    driver.find_element(by=By.XPATH, value='____').click()

-I don't have the credentials to login, but this is how i handle modal alerts

  • Related