简体   繁体   English

无法“关闭”模态弹出窗口

[英]Unable to 'Close' Modal Popup

Im unable to close a modal, when i access my intended URL i get presented with a Modal Popup. 我无法关闭模式,当我访问预期的URL时,出现“模式弹出窗口”。 1. I have tried waiting and clicking on the element 2. also tried close via 'Alert' code 3. all were unsuccessful at closing the popup 1.我尝试等待并单击元素2.也尝试通过“警报”代码关闭3.所有均未成功关闭弹出窗口 在此处输入图片说明 在此处输入图片说明

Thanks for your help, 谢谢你的帮助,

在此处输入图片说明

check , whether it is separate window, if it it then will code might be work 


private static Object Handle1;
private static Object Handle2;

public static void switchToWindowsPopup() {
    Set<String> handles = DriverManager.getCurrent().getWindowHandles();
    Iterator<String> itr = handles.iterator();
    Handle1 = itr.next();
   Handle2 = Handle1;
    while (itr.hasNext()) {
        lastHandle = itr.next();
    }
    DriverManager.getCurrent().switchTo().window(Handle2.toString());
}

public static void switchToMainWindow() {
    DriverManager.getCurrent().switchTo().window(Handle1.toString());

Following code might help you - 以下代码可能会帮助您-

driver.findElement(By.xpath("//span[contains(text(),'CLOSE')]")).click();

or use JavascriptExecuter in this way - 或以这种方式使用JavascriptExecuter

WebElement element = driver.findElement(By.xpath("//button[@class='close']"));
JavascriptExecutor js= (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);

My guess would be that in order to close the dialog, you need to hit the "x". 我的猜测是,要关闭对话框,您需要点击“ x”。 Im assuming this because you are waiting for the button element to become clickable, but it never does. 我认为这是因为您正在等待button元素变得可单击,但它从未如此。 So, id suggest making your locator more specific: 因此,id建议使您的定位器更具体:

@FindBy(xpath = "//button[@class='close']/span")
WebElement closeButton;

Please let me know, if this works..or what happens if it does not. 如果有效,请告诉我,否则无效。

From comment 来自评论

thanks for the fast reply @cathal I get a timeout error that the element is not visible / locator able when the expected time elapses 感谢您的快速回复@cathal我收到一个超时错误,该元素是不可见的/当预期时间过去时,定位器能够

Actually Your locator does not return unique result . 实际上,您的定位器不会返回唯一结果 There are multiple close button element which have same class name close . 有多个close button元素,它们具有相同的类名close

Unfortunately you'r getting invisible dialog close button , that's why you are in trouble. 不幸的是,您将获得不可见的对话框close button ,这就是您遇到麻烦的原因。

You should try something as below :- 您应该尝试以下方法:

WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> elements = wait.until(ExpectedConditions
                .presenceOfAllElementsLocatedBy(By.cssSelector(".close")));
for (WebElement element : elements) {
    if (element.isDisplayed()) {
        element.click();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM