Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium chrome driver click() method not always clicking on elements

I am writing integration tests in c# and when I use the click() method on certain elements inside a dialog box nothing happens and I get no errors. It will click some of the elements inside the dialog but not others. I thought if it wasn't selecting them properly then it would throw and exception but it runs smooth and says test passed even though it never actually clicked the button. The dialog box is an iframe.

I thought maybe it was trying to click a button that wasn't display yet or enabled so I added this before the click() call:

 _driver.SwitchTo().Frame(_frameElement);
     _wait.Until(d =>
    {
      var shippingInfoButton = d.FindElement(By.CssSelector("input[title ='Info']"));
      return shippingInfoButton.Displayed && shippingInfoButton.Enabled;
    });
       var infoButton = _driver.FindElement(By.CssSelector("input[title ='Info']"));
        ScrollToElement(infoButton);
        infoButton.Click();

again this runs with no thrown exceptions so I'm assuming it has found the element and it is both displayed and enabled.

Let me know if you need any more info. Thanks

like image 322
Mitch Avatar asked Jun 14 '13 15:06

Mitch


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.

Why are elements not clickable?

The exception “Element is not clickable at point” might be thrown when the element is not under focus or the action is being performed on the incorrect WebElement. In such cases, you have to switch to the actual element and perform the click action.

What are the different ways to click on any element apart from 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 can't explain why the selenium driver .click() method won't fire on some elements in the page but not others, but I did find a solution.

Using IJavaScriptExecutor you can click the element using javascript instead and in my case it worked.

Here is the code to run the IJavaScriptExecutor and below is my whole method.

    //IJavaScriptExecutor
    IJavaScriptExecutor js = _driver as IJavaScriptExecutor;

         js.ExecuteScript("arguments[0].click();", infoButton);

    //my whole method for clicking the button and returning the page object
    public ShippingMethodDetailsPageObject SelectShippingMethodInfo()
    {
        _driver.SwitchTo().Frame(_frameElement);
        _wait.Until(d =>
        {
         var shippingInfoButton = d.FindElement(By.CssSelector("input[title='Info']"));
            return shippingInfoButton.Displayed && shippingInfoButton.Enabled;
        });

         var infoButton = _driver.FindElement(By.CssSelector("input[title ='Info']"));
         IJavaScriptExecutor js = _driver as IJavaScriptExecutor;

         js.ExecuteScript("arguments[0].click();", infoButton);
        _driver.SwitchTo().DefaultContent();

        return new ShippingMethodDetailsPageObject(_driver, false);
    }
like image 104
Mitch Avatar answered Nov 14 '22 23:11

Mitch