Selenium Click functionality does not work consistently
Meta -
OS:
Windows/Linux (Cent OS & Ubuntu)
Selenium Version:
Any WebDriver version
Browser:
Chrome/Firefox and IE (Any version)
Expected Behavior -
Click operation should always work when no exception is thrown
Actual Behavior -
Click action works something and sometime it does not. It shows the element is actually being clicked, as for link and buttons we can see dotted border around the object. However the action on that click is not triggered. I tried .Click() / Actions Class/ Click using JS/ Press Enter button on element but none is working all the time.
Steps to reproduce -
I wish I could have provided the steps but this is happening on multiple webpages on click action. Many people around world is facing this issue. Save can be checked on stackoverflow or other forums. Element is present on the page and page is completely loaded when I took screenshot. I have seen this issue with Java and Python web drivers.
Suggestions & Observations -
I think selenium is sending the click but browser (IE/FF/chrome) are not ready to process it. May be its binding to JS is not done or something like that. If that is the case then is there a way selenium can find if the browsers are ready to perform click. I guess selenium already check for DOM status. Please see if anything else which can solve the problem
This is a case of waiting for the relevant cues to know when to carry on. We have the WebDriver Wait classes that simplify this for you.
Waits are already present. Wait passes and click does not work randomly with no exception. Page is not changing during this process. If this was the case if Wait class then I'd have got some exception. In this case I am not getting any exception. I request to reopen the bug. I have seen many people across the world are facing this issue are left with no solution.
We will gladly reopen this when you have a test case that shows this issue consistently. Unfortunately until then there is nothing that we can do.
Events not added to a DOM element by the time you click wouldnt throw an error, especially since the web is asynchronous, and the test would try carry on but then not hit the relevant issue.
Hi @AutomatedTester I totally understand the situation you must be facing here. This is one of those scenarios where issue is not reproducible every time making it hard for both of us to move ahead. In such case if someone can help me with where and what to look for, I can gather data as much a possible. I want to understand how and what to look for. I also want to share my observations. Here is an image which has a link "Assign checklist" which takes user to some page. Which works most of the time but sometime. When it does not work it does not have throw any error (coz it has done it job by clicking on it). We can see in the screenshot that, link turns blue when user clicks on it. So click has happened but it did not work. I am clueless why it did not work. was browser not ready to process the request or is it something else? I tried using multiple waits (present/visible/clickable etc) .

It is a known issue that IE clicks can sometimes be flaky. Here is ruby code (You'll need to adapt, that possible could help)
wait_for do
element.click
has_next_page?
end
This will then re-click everytime the next page isn't open, until it is. Not brilliant code, but it gets the job done.
As for FF / Chrome, you're on your own there
Thanks for the suggestion @luke-hill . That was just one of the case. Sometime user moves to different page after click on some element sometime user will remain on same page. Beside, I have some validation added after many click which fails when click does not work. This failure is not due to application error but click did not work so next page did not load. This is kind of false failure.
Exactly the same situation here. I'm running a Firefox on windows 10. It sometimes work and sometimes doesn't, which is driving me crazy.
A workaround: Thread.sleep() for some time before the click, and even before starting to look for the element. This is for the situation where you want to click on something right after refreshing/redirecting the page. Looks like the click might be thrown away, if you try to click while the page is still loading. In my case, it needs to sleep for 6000 ms. Not a good solution, but can get things moving, hope it helps.
@0019 , putting hard coded sleep is not a good solution. I tried 3 seconds of sleep and it did not solve the problem. However, it reduced failures a bit. But, if the DOM status is "completed", then why do we even need hard coded sleep? Is something browser does after DOM is ready? i.e. binding events with Objects? If yes, can we know the status of such thing is completed or not?
@vinaybond , I've upgraded my method to abandon the sleeping. I'm now using the driver as JavascriptExecutor to execute js command to click it. It works fine for me. To make it 100% hit, you may need to use ExpectedConditions to wait for the element to be visible first.
Hi @0019, I tried that as well, but, with that issue of "click not working" increased. I already have checks of element present and visible before I use click using JS.
Here is how I am using JS to click on element. It is written in python:
def myclick(self, locator):
element = self._element_find(locator, True, True)
return self._current_browser().execute_script("arguments[0].click();", element)
I also tried adding hard coded sleep of 2s before clicking. Any suggestion is welcome.
I had a problem similar to vinaybond one and I realise that the reason for the click operation not working was that the element was not completely visible in the windows. I understand this behaviour can be logical but I suggest an exception should be raised for such case. This would have saved me a lot of time to help me understand what was wrong
Here is the workaround: Say I want to click on Link1 and after doing that it will take me to the page which has Button1 on it. Here is the psudo code:
MyClick(Link1, nextloc=Button1, nextAction=Present){
driver.click Link1
if (nextloc is present)
timeout=1 (seconds)
while(timeout<30){
try{
if (nextAction=='present')
driver.wait for element present(Button1, 2second) //if it will fail, it will throw exception
break //exit loop if next element found
} catch{
timeout = timeout+2
driver.click Link1 //Click Again
}
}
}
myClick link1 nextloc=button1
In above example, my action is "element present" (Button1). It can be sometime else. In that case, you have to write a different login.
This has worked well for me
In Chrome I changed from a zoom of 90% to 100% and that solved the problem.
@Edzarius you gotta be kidding me!. The solution was working normally, and i messed with zoom (zooming out). And suddenly everything stopped behaving abnormally.
BTW, how did you find the issue that zoom is the problem. Why is it so. In my code I am using only classnames, not anything remotely related to X,Y Coordinates
@mamohanc this is a long standing issue with chromedriver https://bugs.chromium.org/p/chromedriver/issues/detail?id=628
facing similar issue . Tests fails intermittently due to this. Logs show click operation is passed but button is not clicked, I even tried wait and EC.element_to_be_clickable before performing click , still am facing this inconsistent failure for button click. can someone please share some workaround for this ?
Hi one work around which can be done in this is every time when you want to click any element. 1.Use wait for checking element presence. 2.Before clicking first get page Source and once you get the page source you just search for the element you want to click. 3. Give the check if the element is present in the DOM once you find then click the element . hope this might help to solve the problem.
Hi , I am also faced the same issue, I used try and catch like Vinaybond . Please see the code. In the application the second item get available only when user click the first item successfully.
int timeout=0;
while(timeout<30)
{
try
{
driver.findElement(By.xpath("Xpath of second item")).click();
timeout=30;
}catch(Exception e){
driver.findElement(By.xpath("Xpath of first item")).click();
timeout=timeout+2;
}
}
Ideally writing any logic inside a catch block is not recommended . You should use finally block to get your work done.