Search code examples
javaseleniumselenium-webdriverselenium-java

How can I click a floating advertisement?


I'm trying to move to a different page (click 'my account'), and a floating advertisement appears: advertisement

I tried to click on it, and can't find the "Close" element. Found that it might related to frames, but still not works.

My Code:

public void clickMyAccount() {
    driver.findElement(myAccountBtn).click();
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));

    try {
        Thread.sleep(5);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void clickCloseAd(){
    driver.switchTo().frame(driver.findElement(By.id("google_esf")));
    driver.switchTo().frame(driver.findElement(By.id("aswift_9")));
    driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
    driver.findElement(By.id("dismiss-button")).click();
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
    driver.switchTo().defaultContent();
}

Site: https://practice.automationtesting.in/

Any idea how can I click the floating advertisement?

Tried: move between frames until able to find the Close element Actual: still can't find this element


Solution

  • Found the issue:

    1. My 'wait' not always working, so the driver tries to close the ad when not displayed - still searching for a way to correct this
    2. It's working without frame1: "google_esf", only frame2 and frame3 needed

    Updated code:

            public void clickMyAccount() {
            System.out.println("Click My Account");
    
            if(driver.findElement(myAccountBtn).isDisplayed()){
                driver.findElement(myAccountBtn).click();
                driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
                driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
            } else {
                System.out.println("My account button not displayed");
            }
    
        }
    
        public void clickCloseAd(){
            System.out.println("Click Close Ad");
    
    
                if(driver.findElement(By.id("aswift_9")).isDisplayed()){
    //                driver.switchTo().frame(driver.findElement(By.id("google_esf"))); //Not in use but also a frame
                    driver.switchTo().frame(driver.findElement(By.id("aswift_9")));
                    driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
                    driver.findElement(By.id("dismiss-button")).click();
    
                    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
                    driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
                    driver.switchTo().defaultContent();
                } else {
                    System.out.println("Ad not displayed");
                }
        }
    

    Thanks all!