Ankit Prajapati
Ankit Prajapati

Reputation: 23

How to Handle Popup in Selenium WebDriver

I tried everything from alerts to multiple window handlers but not able to get rid of the popup come when we load below site on initial stage.

https://www.build.com/

Can you please help me on this to just get to above URL and handle the pop up, want to close it straight away.

Thanks Ankit

Upvotes: 2

Views: 759

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

The desired element is within a Modal Dialog Box so to locate/click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

Solution

You need to induce WebDriverWait for the desired 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, "button.close.js-modal-close. > span.close-icon"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='close js-modal-close ']/span[@class='close-icon']"))).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: 1

Naveen
Naveen

Reputation: 788

The pop up you are referring to is not exactly a "Pop Up" window. Its just an element loading in the same page. So, wait till that element appears in the page and click the close button.

# in Java
driver.findElement(By.xpath("//*[@class='close-icon']")).click();

# or in JavaScript
document.querySelector('.close-icon').click();

To wait for any particular element, check this answer

Upvotes: 1

Related Questions