简体   繁体   中英

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. Without the HTML, it's hard to say what the best locator might be but one that might work is the CSS selector,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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