如何使用selenium处理外部弹出窗口

时间:2017-09-06 06:02:46

标签: selenium selenium-webdriver selenium-chromedriver

在我的项目中,有一个广告提供商向我们展示了弹出广告,但问题是这个弹出窗口会不会修复。有时候添加不显示有时会在1分钟后弹出这个并中断我的测试用例。 / p>

我写了一个关闭这个弹出窗口的代码但是我认为这不是一个完美的解决方案。如果有人可以提供帮助?

boolean Imclose = wd.findElement(By.xpath(".//*[@class='IM_overlay_close_container IM_overlay_close_button']")).isDisplayed();
    if (Imclose == true) {
        wd.findElement(By.xpath(".//*[@class='IM_overlay_close_container IM_overlay_close_button']")).click();
    }

3 个答案:

答案 0 :(得分:1)

尝试使用这些代码来处理不需要的弹出页面。我提供了180秒的等待。然后单击广告弹出页面内的关闭按钮。

点击广告弹出页面后,另一个窗口打开,所以我必须重定向到我的主窗口,然后才会执行更多的代码。

popupmenu

答案 1 :(得分:0)

我发现这是网页上的普通弹出式叠加层而不是警报,几乎在60秒内出现。因此,以下代码可能适合您。

//Waiting for the Popup to appear
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@class,'IM_overlay_foreground')]")));

//Clicking on the 'Close' text to close the popup.
driver.findElement(By.xpath("//div[contains(@class,'IM_overlay_foreground')]//span[@class='IM_close_text']")).click();

注意:如果需要更长时间,您可以将时间从60增加到90。事实上,每当弹出窗口出现在时间范围内时,它就会被处理(如在闭包中)。

答案 2 :(得分:-1)

实际上它会在30-50秒后到来,所以你必须等待,然后点击关闭按钮。

System.setProperty("webdriver.chrome.driver", "E:\\software and tools\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.rentbyowner.com/usa");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
WebElement ad=driver.findElement(By.xpath(".//*[@id='IM_target_overlay']/div/div/div/div[1]/a/div"));
WebDriverWait wait= new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(ad));
ad.click();

请参阅此链接以获取更多信息:

Handling Alerts in Selenium Webdriver

相关问题