0

I am trying to click a "Save" button in a pop window using Java Selenium Webdriver, however it throws an exception

Message:Element is not currently visible and so may not be interacted with Command duration

I am able to see the "Save" button active in my pop window. I could not figure out the reason why it throws an exception.

HTML CODE for the Save button that I am trying to click,

</div>
<br>
<br>
<br>
<hr>
<button class="btn btn-primary" style="margin-left: 10px" ng-click="saveData()" data-dismiss="modal" type="button">Save</button>
<button id="buttonmodalcancel" class="btn btn-default" ng-click="cancel()" type="button">Cancel</button>
</div>

firepath: html/body/div[6]/div/div/div[2]/div/div/button[1].

I did not use the XPath as the contents after html/body/div, keeps changing.

Java code that I used:

driver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver().findElementByXPath("//*[contains(text(), 'Save')]").click();
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
ASANT
  • 411
  • 1
  • 6
  • 18

3 Answers3

0

firstly, your code looks wrong. try this one:

driver.manage().timeouts().implicitlyWait(10, "DefaultTimeOutInSec")), TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[contains(text(), 'Save')]")).click();

driver variable (calls) should be used without braces;

secondly, you can use alternative css selector as they work faster:

String buttonCss="button.btn.btn-primary";
driver.findElement(By.cssSelector(buttonCss)).click();

hope this works for you

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
  • @eugene.poschikov, driver() is a custom method that I does the operation of driver, which I inherited from my parent class. But, I still get the same error after using your code. – ASANT Jul 22 '14 at 20:29
  • imo, if it throws element not visible exception the problem is either in xpath (locator ) with which you are locating element, or element has not yet rendered to operate with it. you can use fluentWait for example to wait your button rendered like here: http://stackoverflow.com/questions/12858972/how-can-i-ask-the-selenium-webdriver-to-wait-for-few-seconds – eugene.polschikov Jul 22 '14 at 20:42
  • thanks eugene. Appreciate your response. I provide a name for the operation that I performed in the current pop up window, where the Save button is always active irrespective of the name I provided. – ASANT Jul 22 '14 at 20:49
0

I figured out a way to solve the problem temporarily. Problem I had was the xpath was not always same, it keeps changing in a specified pattern. Sample xpath's that I obtained for an element,

html/body/div[6]/div/div/div/div/div/div/button[1] html/body/div[5]/div/div/div/div/div/div/button[1] html/body/div[2]/div/div/div/div/div/div/button[1]

Since, the number in div was the only variable in my xpath, I used a the below xpath,

driver().findElement(By.xpath("html/body/div[*]/div/div/div/div/div/div/button[1]")).click();
ASANT
  • 411
  • 1
  • 6
  • 18
0

When you get this error, I suggest you to check that the xpath or css selector relates to one and only one element.
I sometimes use a method which clicks on the first displayed element. Basically, it looks like that (you may need to correct it):

public void clickOnFirstDisplayedElement(String path){
    // Retrieve all the elements of the page that make sense with the xpath
    int i = driver.findElements(By.xpath(path)).size();
    for(int x = 0; x < i; x++){
        int y = x + 1;
        boolean isDisplayed = driver.findElement(By.xpath(path)).isDisplayed();
        if(isDisplayed){
            driver.findElement(By.xpath("(" + path + ")[" + y + "]"));
            x = i;
        }
    }
}

Hope it helps.