3

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);
3
  • 1
    That's how your SUT is designed. What's the problem you want to tackle exactly? If you think the design is not testable, you should reach out to the developers - e.g., you can prepare a version on test environments that don't have ads Commented Mar 10, 2022 at 10:07
  • i am getting above ad window, on running the selenium webdriver script . i don't want any advertisement , while my script is running.
    – chinmay k
    Commented Mar 10, 2022 at 12:25
  • Based on screenshots, looks like the SUT is a demo site on guru99.com, so very likely the OP doesn't have access to change anything.
    – Lee Jensen
    Commented Mar 10, 2022 at 23:00

2 Answers 2

1

The ad that you want to close is within and iFrame, which in turn is within another iFrame.

So, first of all you will need to switch to the iFrame within which the ad is displayed.

After you switch to the iFrame you can locate the "Close" button (it's actually a Span tag) an then click() it.

sendKeys("Close") won't work as it isn't a text input element.

Here is my code that works,

try {
    WebDriver driver;
    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//chromedriver");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    Thread.sleep(2000);
    driver.get("https://demo.guru99.com/");
    driver.manage().window().maximize();
    Thread.sleep(2000);
    driver.get("https://demo.guru99.com/V4/");
    driver.findElement(By.name("uid")).sendKeys("<yourusername>");
    driver.findElement(By.name("password")).sendKeys("<yourpassword>");
    driver.findElement(By.name("btnLogin")).click();
    Thread.sleep(2000);
    driver.findElement(By.linkText("New Customer")).click();
    Thread.sleep(5000);
            WebElement frame1 = driver.findElement(By.id("google_ads_iframe_/24132379/INTERSTITIAL_DemoGuru99_0"));
    driver.switchTo().frame(frame1);
    WebElement frame2 = driver.findElement(By.id("ad_iframe"));
    driver.switchTo().frame(frame2);
    driver.findElement(By.xpath("//div[@id='dismiss-button']/div/span")).click();
    driver.switchTo().defaultContent();
    Thread.sleep(5000);
    driver.close();
    driver.quit();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

One thing I noticed in the advertisement is that even though it's a different ad each time, the ID of the div (div[@id='dismiss-button']) remains same. So the same code will work for all ads that appear in the page.

0

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("<css path to the X button>")).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.

2
  • Lee - the issue is, the advertisement is different everytime you run the script , then how to solve , what code i should write, so that no advertisment window is invoked during script running - pls need code help
    – chinmay k
    Commented Mar 11, 2022 at 6:36
  • @chinmayk You can't. You have no control over this. This is a demo site. You need to figure out how to work with this constraint. You have limited options. You can try using an ad-blocker. Chrome extensions can be added during Selenium setup. Or, pick another demo site to learn with.
    – Lee Jensen
    Commented Mar 11, 2022 at 16:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.