Click() method will not always work

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:

  1. HTML content is rendered with the button disabled.
  2. The selenium web driver script was executed before the javascript onload event was triggered (Or finished executing). So the button.click would occur on a disabled element. And nothing would happen.
  3. Then the javascript onload event would trigger (or finish executing) and the javascript would enable the button.
  4. I looked at the page and couldn’t figure out why my code wasn’t working because the button appeared to be enabled upon inspection, and if I manually clicked the button, it worked.

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

Leave a Comment