javaseleniumparsingiframealiexpress

How to close Iframe selenium java


the iframe is the first banner from aliexpress.com I get the local selector to close button, but cant to switch to iframe. Unfortunately, I don't know the id of the iframe. Already try to get elements by selectors "iframe", did not work for me. Is there any way how to get iframe without id, or get the id, or close iframe outside?


Solution

  • You can remove that frame it using Javascript:

    @Test
    public void iframe(){
        driver.get("https://aliexpress.com");
        driver.manage().window().maximize();
        WebElement iFrame = new FluentWait<>(driver)
                .withTimeout(Duration.ofSeconds(10))
                .ignoring(NoSuchElementException.class)
                .until(ExpectedConditions
                        .presenceOfElementLocated(By.xpath("//iframe[contains(@src, 'campaign.aliexpress.com')]")));
        ((JavascriptExecutor)driver)
                .executeScript("arguments[0].remove();", iFrame);
        driver.findElement(By.xpath("//li[@class='product-item'][1]")).click();
    }
    

    P.S. - there are quite a lot of other banners so it is worth having your browser window maximized.