I have a problem with my tests in Selenium WebDriver. The Click
event not always works when a program tries to click on button. In one test everything is ok, in others it is not.
Every test starts from one page. First the user has to choose an option from a select
component and after that the user clicks on a button.
I want to know why one time everything is ok, and when I run tests a second time it is not?
Here is the source code of finding and clicking the button:
public void clickContinueBtn() {
webElement = driver.findElement(By.xpath("//div[@class='btn magenta_s']/a/span"));
webElement.click();
}
I ran into a similar issue. The click method worked on other pages, then didn't work at all on a particular page.
A race condition caused the issue:
button.click
would occur on a disabled element. And nothing would happen.Once I figured out that it was a timing issue, I found the solution here: How can I get Selenium Web Driver to wait for an element to be accessible, not just present?
To paraphrase the solution in Ruby:
//This will not return the button until it is enabled.
button = driver.find_element(:xpath, "//button[@id='myButtonId' and not(@disabled)]")
button.click