Selenium clicks on the element "successfully", but in fact it does not click

I have a method that clicks on a button, however, when it starts, selenium returns the result on a click when the button is actually not pressed. If I run the test several times, occasionally, it will be clicked as expected. I have a test environment installed as an implicit wait of about 15 seconds, I set an explicit wait for this element and still see the same problem. When I do <element>.isDisplayed() , the element is always found. I put a .click in a while loop to click it several times, which works most of the time, however, still sometimes the test fails. Is it possible to have an if statement to check the actual display of an element before clicking a button?

I tried:

 if(!element.isDisplayed){ element.click } 

Here is the button I'm having problems with:

 <button class="notkoButton listNew"> <div class="l6e iconAdd">New List</div> </button> 

Here is my method:

 public marketing_lists_page navigateToNewListPage() throws Throwable { try { int x = 0; while(x < 5) { newListBtn.click(); x++; } //newListPageHeader.isDisplayed(); } catch (NoSuchElementException e){ logs.errorDetails("Could not navigate to New List Page"); Assert.fail(); } return this; } 
+8
source share
8 answers

It looks like the item is not included or not clickable initially. And to answer your question, yes, there is an explicit wait that you can use and wait for the item to be clickable:

 WebDriverWait wait = new WebDriverWait(driver, timeOut); wait.until(ExpectedConditions.elementToBeClickable(locator)); 
+4
source

Try scrolling the item until it clicks. This usually happens when you check on chrome. You can use JavaScriptExecutor to scroll.

Something like that:

 JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("window.scrollTo(0," + element.getLocation().Y + ")"); 
+2
source

Try this, click using javascript and feel free to reposition the item to suit your convenience: -

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

Hope this helps you :)

+2
source

You say you tried explicit wait, but it looks like what you are asking for can be best done with fluentWait (which is a type of explicit wait):

 public WebElement fluentWait(final By locator) { Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(locator); } }); return foo; };; 

Here WebElement will be the button you are trying to click. The cool thing about FluentWait is that the item itself is returned if it is found. From the documentation:

Implementation of the Wait interface, which can have its own timeout and polling interval, configured on the fly. Each FluentWait instance determines the maximum timeout for the condition, as well as the frequency of the status check. In addition, the user can configure wait to ignore certain types of exceptions while waiting, such as NoSuchElementExceptions when searching for an element on the page.

To use it, simply do:

 WebElement newListBtn = fluentWait(By.id("button")); 

Also try:

 Driver.SwitchTo().DefaultContent(); 

before clicking if it is a frame problem.

0
source

Selenium suffers from a lack of dynamism GUI. Thus, I advise you to send the update to the page after clicking.

Tell me what happened.

0
source

I ran into the same problem, but only after I updated my selenium libraries from 2.45 to 2.48.2. The β€œClick” method call is never interrupted, which means that the element is always found by the driver, which, in my opinion, cannot be done, is to right-click. My test will work fine on a higher resolution screen and sometimes skips (but in most cases fails due to click issues) on a lower resolution screen. I tried a lot, but in the end, ZOOM worked for me.

Before starting the test, I call the following method, and the "Click" method works as expected on this low-resolution screen.

 public void zoomOut () { WebElement html = driver.findElement(By.tagName("html")); html.sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT)); } 
0
source

When using JavaScript WebDriver, I had a similar problem - the promise of click () was successful, but nothing happened.

In my case, this was due to the incorrect src attribute of the image on the page (not related to the button that I clicked). For some reason, fixing the image link solved the problem!

I know that the OP used Java, not JS, but my own searches brought me here, so others using JavaScript are likely to go the same way.

0
source

I noticed that clicks are also incompatible with Selenium, to some extent this seems like a known issue:

https://github.com/SeleniumHQ/selenium/issues/4075

In my case, there is practically no way for an item not to be ready. I had an expectation, but more importantly, my test case filled out a very large form, the button that had to be clicked to bring up the drop-down list is very simple, and I see that it loads half a minute before my test. interacts with him, but from time to time he still fails. By the time I wrote a stupid while loop clicking every 100 milliseconds until the expected expansion appeared.

These are all guesses, but from personal observation and some comments of others in this topic - for example, one user mentioned that he noticed more of these problems when running parallel tests, it seems to indicate that this is possible for what seems to be a missed event, and Perhaps this is due to a lag behind its architecture. In particular, I compare this with how Cypress works with its own commands that are sent to the browser directly, without an intermediate server.

0
source

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


All Articles