如何关闭出现在同一页面上的子弹出窗口

时间:2016-09-12 07:11:31

标签: java selenium selenium-webdriver popup webdriver

我想关闭此弹出窗口。它不是警报,不是在框架中而不是在窗口中。我可以关闭它Robot类,但PM说这不是在这种情况下使用Robot类的正确做法。

那我该如何关闭呢?

我尝试了很多次但没有取得成功。

见图: -

https

HTML:

<div id="yui_patched_v3_11_0_1_1473662523684_188" class="page-popup-container-content modal-content yui3-widget-stdmod yui3-widget-content-expanded">
<div id="yui_patched_v3_11_0_1_1473662523684_365" class="yui3-widget-hd modal-header">
<div id="yui_patched_v3_11_0_1_1473662523684_293" class="toolbar-content yui3-widget component toolbar">
<button id="yui_patched_v3_11_0_1_1473662523684_367" class="btn close close-content yui3-widget btn-content btn-focused" type="button">×</button>
</div>
<h3 id="yui_patched_v3_11_0_1_1473662523684_366">Security Tips</h3>
</div>
<div id="yui_patched_v3_11_0_1_1473662523684_344" class="yui3-widget-bd modal-body" style="max-height: 450px; height: 365px;">
<div id="yui_patched_v3_11_0_1_1473662523684_501" class="pop-contain">
<div id="yui_patched_v3_11_0_1_1473662523684_527" style="text-align: center; margin-top: 1em;">
<button id="yui_patched_v3_11_0_1_1473662523684_526" class="lgnBtn btn btnPrimary" type="button" onclick="closepopup1();">Close</button>

   

使用selenium尝试代码: -

WebDriver driver; driver = new FirefoxDriver(); 
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
driver.get("brcp.infoaxon.com"); 

String currentHandle = driver.getWindowHandle(); driver.findElement(By.xpath(".//div[1]/a")).click(); 
Thread.sleep(1000); 
Set<String> handles = driver.getWindowHandles(); 
for (String handle : handles) 
{ 
  if (!handle .equals(currentHandle)) 
  { 
    driver.switchTo().window(handle); 
    driver.close(); 
  } 
  driver.switchTo().window(currentHandle); 
}

2 个答案:

答案 0 :(得分:1)

实际上它不是一个新窗口弹出窗口,它是一个对话框窗口,在点击Read More按钮后会弹出,您应该只是尝试使用By.cssSelector()找到close单击Read More按钮并单击WebDriverWait后按钮,等待此元素可见并可点击,如下所示: -

WebDriverWait wait = new WebDriverWait(driver, 10);

//Click on read more button to open this dialog
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Read More"))).click();

//Now wait until this dialog close button visible and clickable then click  
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.close"))).click();

答案 1 :(得分:-1)

你可以这样做:

    Set<String> handles = driver.getWindowHandles();
    String currentHandle = driver.getWindowHandle();
    for (String handle : handles) {

        if (!handle .equals(currentHandle))
        {
            driver.switchTo().window(handle);
            driver.close();
        }
    }
    driver.switchTo().window(currentHandle);

第二个选项: 这样做:

    WebElement xButton = driver.findElement(By.cssSelector(".close-content"));
    JavascriptExecuter js = (JavascriptExecuter) driver;
    js.executeScript("arguments[0].click()", xButton);