試試這些代碼,以處理不需要的彈出頁面。我提供了180秒的等待時間。然後點擊廣告彈出頁面內的關閉按鈕。
點擊廣告彈出頁面後,另一個窗口打開,所以我必須重定向到我的主窗口,然後纔會執行進一步的代碼。
driver.get("https://www.rentbyowner.com/usa");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
try
{
WebDriverWait wait = new WebDriverWait(driver, 180);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[contains(@class,'IM_overlay_foreground')]"))));
driver.findElement(By.xpath("//div[contains(@class,'IM_overlay_foreground')]//span[@class='IM_close_text']")).click();
try
{
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
//driver.switchTo().window(winHandle).close();
}
driver.switchTo().window(winHandleBefore);
//Verify purpose written code, weather my driver is getting move to parent window or not.
WebDriverWait element = new WebDriverWait(driver, 10);
element.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("top_k_search"))));
driver.findElement(By.id("top_k_search")).clear();
System.out.println("Try block");
}
catch(Exception a)
{
System.out.println("Inner Catch");
}
}
catch(Exception e)
{
System.out.println("Element not present");
//Provide your code here..
}
使用警報界面 – iamsankalp89
請問你能告訴我任何例子嗎? – zsbappa
這個網站是公開的,所以我可以看看你正在處理什麼,它會更容易幫助你。我假設,彈出窗口可能不是一個警報,而是一個iframe /模式。 – Subh