Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Element click has no effect, if the button/link is near the viewport #414

Closed
lukeis opened this issue Mar 2, 2016 · 43 comments
Closed

Element click has no effect, if the button/link is near the viewport #414

lukeis opened this issue Mar 2, 2016 · 43 comments

Comments

@lukeis
Copy link
Member

lukeis commented Mar 2, 2016

Originally reported on Google Code with ID 414

What steps will reproduce the problem?
1. Create a page which renders a button near the viewport.
2. Use WebDriver to find the element and click on it.

I created such a page [1] and also included the JUnit test code.

What is the expected output? What do you see instead?
Click has no effect. The form does not submit.

What version of the product are you using? On what operating system?
selenium-server-standalone-2.0a2.jar, XP, Internet Explorer 7

Please provide any additional information below.
See [1] for an example.

[1] http://sgrunwald.com/simpleForm/

Reported by steffen.grunwald on 2010-03-09 13:32:43

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I've taken a look at the IEThreadElement code.
The getLocationWhenScrolledIntoView method tests if the left upper border of the
element is visible. Only if not, it scrolls it into the view.
The point clicked on the element is the center. But in contrast to the upper left
corner, the center is not tested to be within the viewport.

I think the problem could be fixed if the element is clicked in the center of the
visible part of the element. And looking deeper into the code... in getLocation...
TADAAA:

// TODO(simon): we should clip the size returned to the viewport

That seems to be a good idea and maybe fixes the problem.
But I'm no C++ programmer and we're talking about InternetExplorer so your code might
also be correct and the real reason is the bad weather.

Reported by steffen.grunwald on 2010-03-11 22:39:53

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

OK. I tried to get a debug/dev environment running to fix it myself but I have to
give up, sorry.

Reported by steffen.grunwald on 2010-03-14 21:24:25

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

The problem here isn't the TODO (fixing that will simply return an error when you try

to click something), but is that if the element is scrolled into view and displayed,
we 
assume that the centre is displayed.  What needs adding is code which explicitly checks

whether the point we're about to click is displayed, and if not, scrolls.

Broken on IE, Firefox, Chrome.

Reported by dawagner on 2010-03-15 02:01:25

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Although I think it's more natural to click on the center of the visible part instead
of scrolling to the center of the element first, any fix is appreciated.

Reported by steffen.grunwald on 2010-03-15 05:59:46

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

If it's broken on IE, Firefox, and Chrome, are you talking about a central piece of
code that needs to fixed - something you can point out?

Any known workarounds for the problem?
I would love to use webdriver but I'm afraid I can't use it if the clicks are not
reliable.

Reported by steffen.grunwald on 2010-04-07 09:00:55

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I just ran into something worst...clicking scrolls but the scrolling leads to clicking
the wrong thing or nothing at all.  I am seeing this when trying to click dynamic menu,
it scrolls to the element, enough it is visible, and can click the wrong element. 
In another case, there is dojo dialogbox, which open in the center of screen so always
visible.  But clicking a button, want to scroll to it leading to clicking nothing.
 I see the page scrolling.  I am using Firefox 3.5 and 3.6 and selenium 2.0a5.

Reported by gketan on 2010-08-25 03:04:11

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

can confirm my project is getting the same behavior - if button is visible on form the
click event is executed, but not if the click is supposed to be on a node which is
not visible. using Firefox / IE and 2.0a5 Se with WebDriver.

Reported by dartheth0 on 2010-09-08 16:41:45

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I checked the latest release (2.0a6) with the testcase provided in my initial comment
and it still fails.

Reported by steffen.grunwald on 2010-10-09 08:59:15

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Reported by dawagner on 2010-10-12 16:49:57

  • Status changed: Accepted
  • Labels added: Priority-High, NextRelease, OpSys-All, Browser-Chrome, Browser-Firefox, Browser-IE, Component-WebDriver
  • Labels removed: Priority-Medium

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Reported by dawagner on 2010-10-12 16:50:11

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I fixed this issue in IE by changing the driver to verify the middle of the element
is visible instead of the top-left. This could still cause issues if the element you
want to click is so large that even if moved to the top it's middle is not in the view
port. That seems like an unlikely case to me though.

Reported by roend83 on 2010-10-19 12:27:28


- _Attachment: [0001-Fixed-bug-with-IE-driver-trying-to-click-the-middle-.patch](https://storage.googleapis.com/google-code-attachments/selenium/issue-414/comment-11/0001-Fixed-bug-with-IE-driver-trying-to-click-the-middle-.patch)_

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Interestingly I only have issue with clicking element on scrolling page on Windows but
not Linux. The workaround that I have deployed is to use javascript click rather than
native click event that webdriver provides.

Reported by nhuttan.oz on 2010-11-10 12:15:40

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I can recreate it with Internet Explorer only anyway (at least with the testcase linked
above). @nhuttan.oz Did you observe it in other browsers on Windows as well?

Reported by steffen.grunwald on 2010-11-10 12:35:11

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

@steffen I observed it in Firefox 3 on Windows 7 using selenium-java-2.0a2

Reported by nhuttan.oz on 2010-11-15 20:05:17

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

i also managed to reproduce the issue on firefox 3.6 on windows server 2008.

Reported by ChristophKaser on 2010-11-16 19:27:53

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I managed to build a testcase for the bug on firefox. I think it is not exactly the
same as the internet explorer bug:

Open the attached html-file with the firefox driver, and let it click on the link.

It will not work, due to the line wrapping. 
I suspect that it tries to click on the center of the bounding box of the link, which
is actually not part of the link.

Reported by ChristophKaser on 2010-11-17 11:24:19


- _Attachment: [test.htm](https://storage.googleapis.com/google-code-attachments/selenium/issue-414/comment-16/test.htm)_

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Christoph, you are correct. I can recreate the mentioned error too. I included it in
the testcase above and updated the JUnit code.

The testcase proves that this one is not dependent on the position relative to the
viewport. That's why I created an additional test method for it. I suggest it should
be filed as a separate issue for all browsers (I tested FF 3.6 and IE 8).

Speaking of browsers, I still can't provide a valid testcase for Firefox and the viewport
problem.

Reported by steffen.grunwald on 2010-11-17 18:24:20

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

in wrappedElement.js (webDrive.xpi, selenium 2.0a7)

 if (this.enableNativeEvents && nativeEvents && node && useNativeClick && thmgr_cls)
{
    Logger.dumpn("Using native events for click");
    var loc = Utils.getLocationOnceScrolledIntoView(element);
    var x = loc.x + (loc.width ? loc.width / 2 : 0);
    var y = loc.y + (loc.height ? loc.height / 2 : 0);
...

This code call the method element.getBoundingClientRect().
It works well with block elements like "div".
But to inline elements like "a", how about using "getClientRects" ? 

Thanks

Reported by temp.you on 2010-11-19 03:08:45

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

excellent suggestion, temp.you, I just tried it and it works!

Reported by ChristophKaser on 2010-11-19 08:49:19

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I created a new issue for this problem: 1020
http://code.google.com/p/selenium/issues/detail?id=1020

Reported by ChristophKaser on 2010-11-19 08:55:58

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Issue 1020 should be the same as issue 835:
http://code.google.com/p/selenium/issues/detail?id=835

Reported by joychester on 2010-11-19 09:05:57

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Issue 835 indicates it works with IE, while issue 1020 actually does not work with IE8.

Reported by steffen.grunwald on 2010-11-19 14:45:25

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Issue 1190 has been merged into this issue.

Reported by dawagner on 2011-01-12 22:33:46

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Reported by dawagner on 2011-01-12 22:34:06

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Issue 1186 has been merged into this issue.

Reported by simon.m.stewart on 2011-01-20 16:50:27

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Issue 707 has been merged into this issue.

Reported by simon.m.stewart on 2011-01-21 14:00:23

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

simple workaround that I found working: you can use javascript call from RemoteWebDriber
to change window size or move viewport, so that link becomes clickable.
Eg. 
driver.executeScript("window.moveTo(0,0); window.resizeTo(screen.width,screen.height);");

Reported by konopski on 2011-02-06 21:02:30

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Not sure if this issue is related, but it seems very similar:
http://stackoverflow.com/questions/5574802/selenium-2-0b3-ie-webdriver-click-not-firing

So basically Click() on an element fails occasionally and unpredictably if the browser
does not have focus.

Reported by duncanscott69 on 2011-08-19 09:48:31

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I don't think that is the same bug. I have seen webdriver scroll to an element and actually
scroll further then it should (pixel position of element incorrectly calculated). Afterwards,
trying to interact with another element just off the edge of the screen that would
have been visible if webdriver scrolled to the correct pixel position for the first
element causes an error since it thinks its visible but it is not.

Reported by patrickbowie on 2011-08-19 14:06:11

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I'm not sure if this is the same bug or not, but I've been having a very similar problem.
 Sometimes, for no apparent reason, an element.Click() event will have no effect. 
However, element.SendKeys() will work perfectly on that same element instance.

I've noticed that, whenever this occurs, the browser scrolls down first, presumably
so that the element will be on the screen.  However, in my case, the entire element
is visible when this happens.  Instead, what happens is that only the top portion of
the button is on the screen initially.  Then, when Click() is called, the browser scrolls
down so that th button is 100% visible.  Then, nothing happens.  The click doesn't
actually take place.  No hanging or errors, just skipped.

I've been in debug mode trying to see if there's any possible way to get the button
to click, aside from using SendKeys().  So far, nothing.

Also, I have the browser maximized when the test first starts.  While I'm sure that
reduces the risk of this happening, it doesn't eliminate it.  Even when the browser
is maximized, it's not uncommon for there to be vertical scrolling, and for an element
to be only partially visible at the bottom of the viewport.

Has there been any recent activity on this front that anyone knows of?  If not, would
it be possible for me to check to see whether or not Click() actually succeeded?  It
doesn't have a return value and it doesn't throw any exceptions on failure as far as
I can tell.  But if I can capture that somehow, then I could at least create a workaround
that would call SendKeys(" ") if the Click() fails.

Reported by kris.craig on 2011-10-03 22:04:47

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I've just discovered that the SendKeys("\n") workaround doesn't work on checkboxes (using
spacebar didn't work either).  At present I can't think of any other workarounds to
get that box checked.

Reported by kris.craig on 2011-10-03 23:20:35

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Clicking elements on the border or outside the viewport works in the latest ChromeDriver.
Removing Chrome label...

Reported by kkania@google.com on 2011-10-04 17:56:33

  • Labels removed: Browser-Chrome

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I tested with latest releases and couldn't recreate the problem on Internet Explorer
with any release >= 2.0.0 (but not including 2.0a6).
Case closed for IE it seems.

Reported by steffen.grunwald on 2011-10-04 19:44:36

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I believe this is fixed in Firefox as well, since we now have code to scroll the clicked
location into view.

Reported by eran.mes on 2011-11-02 23:21:52

  • Status changed: Fixed
  • Labels removed: Browser-IE

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I have this problem in IE8 and Selenium Standalone Server 2.19 on XP. Can't click on
link which is partly out of ie view area (at the right side of browser under scroll
bar).

But the more interesting that following code doesn't work either:

                Actions builder = new Actions( getWD() );
                Action hover = builder
                        .moveToElement( _obj, 5, 5 )
                        .build();
                hover.perform();

(5,5) point of element is visible.

click is performed fine if I manually scroll horizontal view to "logout"

Reported by SirVolant on 2012-03-02 04:45:13

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Let me clarify the problem I am having in IE9, WebDriver v2.21.  The following is a
sampling of the approaches I have taken.  I have tried many variations of each!

--- driver.findElement(By.xpath(seleniumLocator)).click()
     Often has no effect

--- new Actions(driver).moveToElement(element).clickAndHold().moveByOffset(1,1).release().perform()
(and MANY variants of same)
     Sometimes has no effect

--- driver.findElement(By.xpath(seleniumLocator)).sendKeys(Keys.ENTER)
     Sometimes triggers the stale element exception and
     sometimes does nothing but prints "We don't have focus on element." to the console

--- ((JavascriptExecutor) driver).executeScript("return arguments[0].click()", element)
     By far the most successful approach I have used, as it always successfully clicks
the element.  BUT it does not return when the click triggers a modal dialog.  (The
other approaches detailed above do not have that problem - on the occasions that they
successfully click the element, they return even when the nodal dialog is triggered.)

I have not tried calling WebElement.submit(), because the problem occurs with many
elements that are not part of a form

Note that I experience this erratic behavior with many elements.  Things that most
or all of those elements have in common:
   They do NOT wrap the screen
   They do NOT need to be scrolled into view
   They do NOT have odd viewport characteristics
   They are mostly incredibly simple elements, for instance:
  <input type="submit" value="Login" name="Login" id="Login" class="loginButton">

Things that all observed failures have in common:
   All fail in IE9 (have not tried another version, but that's a moot point - I have
to support IE9
   The browser is zoomed at 100%
   The browser's being maximized or not is not a factor
   Timing is not an issue - it happens just as often when stepping through the code
as not

I have done more than a little research, and have tried pretty much every approach
that is purported to work.  However, I have yet to find an approach that works consistently.
Does anyone know of a rock-solid workaround?   

Reported by jeffreyablaze on 2012-05-09 04:55:44

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

hi jeffrey, 
i am facing the same issue. Did you get any solution for the same.
Thanks in advance

Reported by kirankumar.ngp on 2012-09-04 06:07:36

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

@jeffreyablaze, without a test case there's not a lot we could do. Please provide an
HTML snippet and test code in the language of your choice that reproduces your problem.
Also, please open a new issue for this.

Reported by eran.mes on 2012-09-11 12:16:26

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Same here on MacOS with Chrome, a possible clue was that the link was partially hidden
behind the address bar that appears at the bottom of the Chrome window.

Reported by nicolas@guillaumin.me on 2012-09-11 23:22:08

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Hi,

I have the same problem with Firefox, the radio button is under the drop down. Please
let me know if you have any solution.

Thank you in advance,

Reported by IrinaV2012 on 2012-10-12 18:20:39

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Hi, 

    I am facing a similar issue.a button is outside the view area and when i scroll
down the button is seen.Is is possible to click on the button without having to scroll?

Thanks,
Sanam

Reported by sanam.chugh@qualitiasoft.com on 2013-03-05 04:32:39

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I've found, on MacOS (10.8) with Chrome, that switching "Show scroll bars" to Always
(in System Preferences -> General) seems to fix the occurrences of this problem I was
seeing.

Presumably the problem was caused by the fact that the scroll bar would magically appear
over some page content when the 'click' occurred near the edge of the window, where
as changing it to always show causes a specific area to be allocated for the scroll
bar which doesn't have any content behind it (and hence is never clicked).

Reported by matthew.sheppard on 2013-03-05 04:46:50

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Reported by luke.semerau on 2015-09-17 17:48:57

  • Labels added: Restrict-AddIssueComment-Commit

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant