Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click() method will not always work

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();  } 
like image 210
user1494328 Avatar asked Aug 28 '12 13:08

user1494328


People also ask

Why click method is not working in Selenium?

We can list the most common reasons for click problems as being one of the following: Wrong web element locations. The existence of a web element that obscures the web element that we want to click. The Selenium WebDriver works much faster than the response of the application.

What you do when click function not working in Selenium script?

Selenium clicks on span instead of the button. That is why click() is not working. Please use an Id or name if available or change the xpath to include the button tag as well. If you can post the html it will be easy to create the xpath.

What is click () method in Selenium?

For automating the right-click operation, Selenium provides a dedicated method – contextClick(). This method accepts the target WebElement as the argument. In order to use this method, use the Actions class object. The method of locating the desired element remains the same.

What is the alternative for click method in Selenium?

Alternative of click() in Selenium We can use the JavaScript Executor to perform a click action. Selenium can execute JavaScript commands with the help of the executeScript method. The parameters – arguments[0]. click() and locator of the element on which the click is to be performed are passed to this method.


1 Answers

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 
like image 176
Walker D Avatar answered Oct 14 '22 14:10

Walker D