Heyjava
Heyjava

Reputation: 47

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!

Source

Upvotes: 1

Views: 4239

Answers (4)

Elliott Weeks
Elliott Weeks

Reputation: 83

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.

Upvotes: 1

KunduK
KunduK

Reputation: 33384

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.

Upvotes: 2

frianH
frianH

Reputation: 7563

Try the bellow xpath:

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

Upvotes: 0

Somber
Somber

Reputation: 443

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

Upvotes: 1

Related Questions