3

I cannot click on this button contained in a modal

this is the html

<div class="col-md-6 col-sm-6 col-xs-12">

    <button class="btn modal-button full-btn" id="saveexit" type="button">
Save and Exit</button>

</div>

Here is the java code:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"saveexit\"]")))

I have also tried with:

cssSelector: #saveexit
cssPath: html.no-touch body div.remodal-wrapper.remodal-is-opened div.modaliAdesione.remodal.remodal-is-initialized.remodal-is-opened 
div.modal-dialog div.modal-content.modal-custom-content div.modal-footer div.row.text-center div.col-md-6.col-sm-6.col-xs-12 
button#saveexit.btn.modal-button.full-btn
xpath: //*[@id="saveexit"]

this is the error I am getting:

Expected condition failed: waiting for visibility of element located by ...

I also asked a similar question on StackOverflow

I am new to Selenium so excuse me if it is a silly question. Here is an extract of the html code: https://codeshare.io/arLW9q

Please note: if I run document.getElementById('saveexit').click(); from browser's console it works out

13
  • Is the modal window visible when the code is called? Commented Dec 27, 2017 at 17:26
  • @SlightlyKosumi yes it is
    – eeadev
    Commented Dec 27, 2017 at 17:34
  • 1
    thanks @SlightlyKosumi, driver.findElements(By.id...) is returning a list of 0 elements :(
    – eeadev
    Commented Dec 28, 2017 at 11:21
  • 1
    Based on the HTML you posted, I can't see any reason why the locator you're using wouldn't work. Something else to check is whether your driver's window/frame context is correct. If the HTML you provided is within an iFrame, you must switch the driver context to that frame before trying to locate the button. Ditto if the modal is (for some reason) opened in a new window. Commented Dec 28, 2017 at 15:44
  • 1
    I don't work much with Java Selenium, but I think getWindowHandles() will give you what you need. Commented Dec 28, 2017 at 16:42

2 Answers 2

4

Possibilities

I can't see anything wrong with the snippet you shared, but here are a few things to check out.

  • The modal is inside of an iFrame. You need to switch focus to the iFrame first before you can read/manipulate anything in it. Can you fill in any of the fields in the same modal?
  • There is actually more than one id="saveexit" on the page and the first one is hidden so the selenium is correct. Search the whole source for that string just to be sure (coders love cut and paste and may have put the same ID twice).
  • Switch from: By.xpath("//*[@id=\"saveexit\"]")) to By.xpath("//*[@id='saveexit']")) - Not sure if it'll make a difference, but it takes out some confusion.

P.S. iFrame is not the same as window. You need to use the correct methods.

To count

driver.findElements(By.xpath("//iframe")).size();

To access

 driver.switchTo().frame(iFrame);
5
  • unfortunately size of iframe list is 0 length
    – eeadev
    Commented Dec 29, 2017 at 9:22
  • not even switching the xpath differently works out
    – eeadev
    Commented Dec 29, 2017 at 9:33
  • if I run document.getElementById('saveexit').click(); from browser's console it works out
    – eeadev
    Commented Dec 29, 2017 at 9:43
  • @eeadev, since your question is about wait, what happens if you an explicit wait? Just wait for a certain number of seconds and just click. If it's clickable, you can change the wait to wait.until(ExpectedConditions.elementToBeClickable(button)); and simplify things.
    – MivaScott
    Commented Dec 29, 2017 at 17:18
  • it happens that button is never found
    – eeadev
    Commented Jan 2, 2018 at 15:31
0

As per the html code https://codeshare.io/arLW9q you have shared, below are few techniques that we use in the automation testing company for element click:-

1.driver.findElement(By.ID("saveexit")).click();

2.driver.findElement(By.xpath(//div//button[@id="saveexit"])).click(); or driver.findElement(By.xpath(//div//button[contains(text(),"SAVE EXIT"])).click();

3.driver.findElement(By.cssSelector("div.col-md-6.col-sm-6.col-xs-12 > button#saveexit"));

Note: Further if you are able to run document.getElementById('saveexit').click(); from browser's console than you can click on the element by javascript:

import org.openqa.selenium.JavascriptExecutor;

((JavascriptExecutor) driver).executeScript("document.getElementsByID('saveexit').item(1).click();");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.