I am trying to login to the website using Selenium Webdriver. But "http://www.letskodeit.com/" when I am opening this site an ad is popping up. I am unable to close the the ad. I need help to sort it out. Thanks in advance.


Below is my example of the method for closing your banner:

public static void closeAd(WebDriver driver){
    Wait adWaiter = new FluentWait(driver)
    WebElement closeButton = (WebElement) adWaiter.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver o) {
            return o.findElement(By.xpath("//div[@role='button'][@title='Back to site']"));
    System.out.println("Banner closed..");

And here is the example that is showing that the method is working fine:

public static void main(String[] args) throws InterruptedException {
    System.setProperty("webdriver.chrome.driver", "E:/Dev/WebDrivers/chromedriver.exe");
    WebDriver driver1 = new ChromeDriver();

I added Thread.sleep(10000) at the end just to make the test more representative.

P.S. - In your question your saying about unexpected ad. However it is not that straightforward how to deal with unexpected events on the site under test. So this code implies that you expect the banner will appear.

To make your test work with both cases when banner might appear and might not appear you can wrap closeAd method invocation with try..catch block and catch org.openqa.selenium.TimeoutException. In this case your test will anyway be hanging for a timeout period before proceed.

| improve this answer | |
  • thank you very much. It is working for me. – AVIJIT DAS Nov 21 '18 at 11:34

Please find the code

public class PopUp {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
        WebDriver driver = new ChromeDriver();


Hope this is helpful.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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