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

I am writing integration tests in C #, and when I use the click () method for some elements inside the dialog box, nothing happens and I don't get errors. It will click some elements inside the dialog box, but not others. I thought that if he didn’t choose them correctly, he would throw and exclude, but he works smoothly and says that the test passed, even if he didn’t actually press a button. The dialog box is an iframe.

I thought that maybe he was trying to click a button that was not yet displayed or turned on, so I added it before calling click ():

_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 without exception, so I assume that he found this element and it is displayed and activated.

Let me know if you need more information. Thanks

+4
source share
3 answers

I cannot explain why the selenium driver.click () method does not start some elements on the page, but not others, but I found a solution.

Using IJavaScriptExecutor, you can click an element using javascript, and in my case it worked.

Here is the code to run IJavaScriptExecutor and below - 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); } 
+6
source

I had a similar problem. If the same problem has an error in ChromeDriver, it cannot click some elements due to surrounding divs, etc. A bit lame indeed.

A simple fix is ​​to send the Enter key, for example. element.SendKeys (Keys.Enter). Seems to work in all browsers.

+4
source

I have several tests that work in Firefox all the time, and in Chrome it drove me crazy, because sometimes it was successful, and sometimes ".click" didn't work, and it didn't work.

It took a long time to notice this, but the reason is that I sometimes reduced the browser to 80% to see the browser next to my IDE. It seems that ".click" does not work when I did this.

At least for me it was a problem

0
source

Source: https://habr.com/ru/post/1486270/


All Articles