Selenium + Java: Unable to close Advertisement in webpage



  • Issue I am facing:

    After this step: driver.findElement(By.linkText("New Customer")).click(); an advertisement window opens and script stops here. I'm unable to close the advertisement and continue further navigation/testing.

    I'm still learning and hence not sure what I should do. I need help with this situation.

    I ordered to stop the advertisement window during whole program (don't want Advertisement at all).

    Advertisement is different every time I run the script. enter image description here

    enter image description here

    package selenium;
    

    import java.sql.Array;
    import java.util.Arrays;

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;

    public class gure99 {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://demo.guru99.com/");
        driver.manage().window().maximize();
        driver.get("https://demo.guru99.com/");
        driver.manage().window().maximize();
        driver.findElement(By.name("emailid")).sendKeys("<my username>");
        driver.findElement(By.name("btnLogin")).click();
        driver.get("https://demo.guru99.com/V4/");
        driver.findElement(By.name("uid")).sendKeys("mngr385415");
        driver.findElement(By.name("password")).sendKeys("<my password>");
        driver.findElement(By.name("btnLogin")).click();
        driver.manage().window().maximize();
        driver.findElement(By.linkText("New Customer")).click();
        Thread.sleep(5000);
        driver.switchTo().alert().sendKeys("Close");
        Thread.sleep(5000);
    



  • Based on the screenshots and the code supplied, it looks like the OP is learning Selenium by using a demo site on guru99.com.

    While the code doesn't say where the pop-up is attempting to be closed, I have a feeling the attempt is on driver.switchTo().alert().sendKeys("Close");

    This won't work. The alert() method is meant to switch the driver focus to a browser based alert, treating it like a new browser window. The pop-up in the image is NOT that type of alert, so Selenium doesn't know how to find it to close it.

    The pop-up does look like a normal JavaScript modal. It has an 'X' button, an open button, a snowman menu (3 vertical dots). The solution is to find an element locator for the 'X' button and click it.

    Something like, a bit of pseudo code so it won't work exactly:

    driver.findElement(By.cssSelector("")).click();
    Thread.sleep(5000);
    

    Other than CSS Selector, you can also use an Xpath.

    Normally, I wouldn't recommend a sleep, but to use a built-in wait (explicit wait) method that Selenium has.

    Given that the OP seems to be learning, their current format is fine for this. I do want to encourage learning next steps which are to learn about Page Objects and to setup a solution using the Page Object Method.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2