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

Java 如何关闭显示在同一页面上的子弹出窗口,java,selenium,selenium-webdriver,popup,webdriver,Java,Selenium,Selenium Webdriver,Popup,Webdriver,我想关闭这个弹出窗口。它不是一个警报,不是在框架内,也不是一个窗口。我可以关闭它Robot类,但是PM说在这种情况下使用Robot类不是正确的做法 那我怎么才能关闭这个 我试了很多次,但都没有成功 如图所示:- HTML: 您可以这样做: Set<String> handles = driver.getWindowHandles(); String currentHandle = driver.getWindowHandle(); for (String h

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

那我怎么才能关闭这个

我试了很多次,但都没有成功

如图所示:-

HTML:


您可以这样做:

    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);

您可以这样做:

    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);

实际上,它不是一个新的弹出窗口,而是一个对话框窗口,单击
readmore
按钮后将弹出,您只需尝试使用
By.cssSelector()
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();

实际上,它不是一个新的弹出窗口,而是一个对话框窗口,单击
readmore
按钮后将弹出,您只需尝试使用
By.cssSelector()
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();


哦从图片中我可以看出这不是弹出的。很抱歉你能找到这个“对话框”的x按钮吗?没有错误。它过去了,但弹出窗口没有关闭。我正在使用这个代码WebDriver;驱动程序=新的FirefoxDriver();driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);driver.get(“);String currentHandle=driver.getWindowHandle();driver.findElement(By.xpath(“.//div[1]/a”)).click();Thread.sleep(1000);Set handles=driver.getWindowHandles();for(String handle:handles){if(!handle.equals(currentHandle)){driver.switch().window(handle);driver.close();}.switch().window(currentHandle);})}@KhushbooChaudhary编辑您的问题,并尝试将您的问题替换为评论请查看我的第二个选项哦…从图片中,我可以看到这不是弹出的。对不起。您能找到此“对话框”的x按钮吗?没有错误。它已传递,但弹出窗口未关闭。我正在使用此代码WebDriver;driver=new FirefoxDriver();driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);driver.get(“);字符串currentHandle=driver.getWindowHandle();findElement(By.xpath(“.//div[1]/a”)。单击();睡眠(1000);Set handles=driver.getWindowHandles();对于(字符串句柄:句柄){if(!handle.equals(currentHandle)){driver.switchTo().window(句柄);driver.close();}driver.switchTo().window(currentHandle);}}@KhushbooChaudhary编辑您的问题,并尝试对您的问题而不是评论进行编辑请查看我的第二个选项它实际上不是窗口弹出窗口,这只是一个消息对话框,如果你想解决这个问题,你也需要共享这个HTML。好的,用提供的答案试试,让我知道。事实上,这不是一个弹出窗口,这只是一个消息对话框,如果你想解决这个问题,你也需要共享这个HTML。好的,用提供的答案试试,让我知道。代码正在运行,谢谢。我尝试了使用代码驱动程序.findElement(By.xpath(//button[contains(@class,'lgnBtn btn btnPrimary')))))。单击()并处理此问题。我认为问题在于XPath。前面我使用了由Firepath生成的XPath。Firepath确实为locate元素创建了问题吗?由此,driver.findElement(By.id(“yui_patched_v3_11_0_1_147366252684_526”)。单击();它不起作用。。我不知道为什么?@KhushbooChaudhary为什么你不使用
cssSelector
来尝试我建议的答案呢??而reason
By.id()
不起作用,因为它在每次启动时都会动态更改..:)您需要使用
By.cssSelctor()
来代替建议的答案..谢谢..:@KhushbooChaudhary,实际上,firepath只提供了位置
xpath
,这在动态情况下是不稳定的,这就是您无法定位元素的原因。如果这个答案有助于解决您的疑问,还需要代码,谢谢。我尝试了使用代码驱动程序.findElement(By.xpath(//button[contains(@class,'lgnBtn btn btnPrimary')))))。单击()并处理此问题。我认为问题在于XPath。前面我使用了由Firepath生成的XPath。Firepath确实为locate元素创建了问题吗?由此,driver.findElement(By.id(“yui_patched_v3_11_0_1_147366252684_526”)。单击();它不起作用。。我不知道为什么?@KhushbooChaudhary为什么你不使用
cssSelector
来尝试我建议的答案呢??而reason
By.id()
不起作用,因为它在每次启动时都会动态更改..:)您需要使用
By.cssSelctor()
来代替建议的答案..谢谢..:@KhushbooChaudhary,实际上,firepath只提供了位置
xpath
,这在动态情况下是不稳定的,这就是您无法定位元素的原因。如果这个答案有助于解决您的疑问,您还需要
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();