简体   繁体   中英

Selenium webdriver - Unable to locate element on modal dialog

I cant locate a button on dialog pages, I tried to use cssselectors, xpaths, but simple i cant locate buttons/texts on modal dialogs.

I attached a screenshot from the code.

What can you recommend?

Thank you!

资源

You could try this:

        JavascriptExecutor js= (JavascriptExecutor) driver;
        WebElement webElement=driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default"));
        js.executeScript(“arguments[0].click()”, webElement);

Hope it helps.

By.xpath(".//button[.='/"Submit/"']) 

or

By.xpath(".//button[@class='btn btn-default']) 

If it found but click doesnt work try that javascript from other comment

尝试下面的xpath

driver.findElement(By.xpath("//div[@class='modal-footer']//button[contains(@class,'btn-default')]")).click();

try Alert alert = driver.switchTo().alert(); alert.accept(); Alert alert = driver.switchTo().alert(); alert.accept();

or

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); before your actual code for modal window to load

or if these dont work, can you share the error in your console

I presume you are able to identify the element.However unable to click on that. Try use following options.

  1. Use WebDriverWait and elementToBeClickable to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
elementBtn.click();
  1. Use Action class to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
Actions action=new Actions(driver);
action.moveToElement(elementBtn).click().build().perform();

  1. Java Script Executor to click on the element.
JavascriptExecutor js= (JavascriptExecutor) driver; 
js.executeScript("arguments[0].click();", driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default")));

Note : If above all options doesn't work.Check if there any iframe avaialable.If so, you need to switch to iframe first.like below.

driver.switchTo().frame("framename"); //name of the iframe.
driver.switchTo().frame(0); //you can use index as well.

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