简体   繁体   English

无法单击 Selenium Webdriver (Java) 中模态的“关闭”按钮

[英]Not able to click "Close" button of a Modal In Selenium Webdriver (Java)

After clicking on a link a modal appear where a close button exist.单击链接后,会出现一个模式,其中存在关闭按钮。 I tried to close the modal with below code but it's not working.我试图用下面的代码关闭模态,但它不起作用。

WebElement element = driver1.findElement(By.className("btn btn-secondary"));

if (element.isEnabled()) {
  element.click();
} else {
  System.out.println("Disable");
}

I can tell from your locator that you are at least getting the error,我可以从您的定位器中得知您至少收到了错误消息,

Compound class names not permitted不允许复合类名

By.className() expects a single class name but you have provided two which will cause the error above. By.className()需要一个类名,但您提供了两个会导致上述错误的类名。 Without the HTML, it's hard to say what the best locator might be but one that might work is the CSS selector,没有 HTML,很难说最好的定位器可能是什么,但可能有效的是 CSS 选择器,

By.cssSelector(".btn.btn-secondary")

As generic as this is, it's possible that there is more than one element that matches.尽管这是通用的,但匹配的元素可能不止一个。

You might need to add a wait, especially since you are dealing with a modal dialog,您可能需要添加一个等待,特别是因为您正在处理一个模态对话框,

new WebDriverWait(driver).until(ExpectedConditions.elementToBeClickable(...)).click();

You probably want another wait to make sure that the dialog closes before the script continues.您可能需要再次等待以确保在脚本继续之前对话框关闭。

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

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