Click() method will not always work

2020-05-13 03:58发布

问题:

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(); 
}

回答1:

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


回答2:

You can also try using the Javascript based alternative method for clicking. The code for this can be as follows:

WebElement element = driver.findElement(By.id("something"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);


回答3:

Many times it happens because of Browser compatibility (Mostly on firefox). So try to use "WebElement.sendKeys(Keys.ENTER);" code instead of "webElement.click(); "



回答4:

Have you made sure, there is no timing issue? Make a break point before finding the element and look if your code runs fine. If there is a timing issue, use a an explicit Wait before clicking your button. Note also, that the button should be clickable (element must be visible AND enabled) before actually clicking it.

a solution with explicit wait could look like this:

By by = By.xpath("//div[@class='btn magenta_s']/a/span");
WebDriverWait w = new WebDriverWait(driver, timeout);
WebElement element = w.waitUntil(ExpectedConditions.elementToBeClickable(by);
element.click();


回答5:

Try Out below code sometimes you have deliberately move your focus towards your elements

WebElement element = driver.findElement("yourElement");
        Actions builder = new Actions(driver);
        builder.moveToElement(element).click(element);
        builder.perform();


回答6:

Looking at your xpath you have not identified the button, instead you are pointing to the span tag inside which button is present. 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. click() has been working perfectly for me and I have been using this method for over an year in a project and every time it works fine...



回答7:

I experienced a similar problem a couple of days ago and I found the solution in my particular case. ===Using Selenium Webdriver, I wanted to click to the link of "First Link" and that would load content on the page.

Below a portion of code with structure alike my case:

<li class="first-link">
    <a class="common-style" href="javascript:;" style="padding-left: 15px; padding-right: 15px;">First Link</a>
</li>

===The result is that the Webelement is supposed to be found and clicked but switching to the UI, nothing happened, no error was thrown either (element not found, element not ready for click, element disabled, etc).

After trying a couple of different ways to find the link through (xpath and css, didnt try by id because in my case there is no unique id), I was able to access and click the element with Selenium webdriver by css with the following value: li.first-link a. However, when I tried to access by xpath with the following value, this was 'found' in Firefox but click didnt work: .//li[a/text()='First Link'].

The problem was a slight xpath syntax issue, that firebug from Firefox didnt report at all, in fact it found the link.

I changed the braces order like this: .//li/a[text()='First Link'] Now it works as expected, First Link is found and clicked and the javascript code that loads the page is fired.

My findings are: * It is not a timing issue, so this does not happen because you try to click an element that is not ready. Otherwise we had solved this with an explicit Selenium wait...

  • The problem is not that element is found and the javascript is not fired. You can try to execute directly javascript code and fire manually the events and see this does not work.
  • This issue happens on Firefox 22,probably works for older versions from this browser. I cant provide information if this works on IE or Chrome. The issue itself is that even that there is a syntax conflict on xpath firefox does not throw an exception, that makes you think there is nothing wrong with your code, but there is.
  • This is an unexpected unhandled behavior for Firefox. I have found a bug reported for this: http://code.google.com/p/selenium/issues/detail?id=4757

SOLUTION SUGGESTED: verify your html structure, your xpath, try to find issues in the syntax, find different ways to access to the element.



回答8:

One possible reason for this could be click() method is not able to dispatch click event on every element.

Hence, you could explicitly fire click event on the element you are trying to click.