1

I am using Webdriver to drive the browser for testing our website. However, I am running into problems where Webdriver is unable to perform operations on a page element, unless I scroll down the page using something like driver.findElement(By.id("myElementId")).sendKeys(Keys.ARROW_DOWN);

My question is, does the WebElement always have to be displayed on the page for any operation (such as click()) to be performed? Shouldn't it be sufficient that the element is present in the DOM after the page is loaded?

rs79
  • 2,311
  • 2
  • 33
  • 39

3 Answers3

3

From the WebElement documentation for Click()

There are some preconditions for an element to be clicked. The element must be visible and it must have a height and width greater then 0.

Selenium WebDriver simulates user interaction with the web page, so it can't click on the element if it's not displayed (although "exist in the DOM" is sufficient for locating elements and extract data like text). Clicking on not-visible element might result in an error such as

ElementNotVisibleException

Or

Element is not clickable at point (411, 675). Other element would receive the click

Or unexpected behavior as another element does receive the click without any error.

That's why selenium provides many solutions to overcome this problem. For example:

  • Maximizing the window in the beginning of the test
  • Scrolling to elements you want to interact with using moveToElement method in Actions
  • Explicit wait with expected conditions like visibilityOfElementLocated
Guy
  • 46,488
  • 10
  • 44
  • 88
2

Selenium WebDriver aims to mimic the behavior of the real user, and as such interacts with the HTML of the application. To interact with the element it is important that the element is visible. According to WebDriver W3C Specification, the visibility of an element is guided by what is perceptually visible to the human eye. So the answer is YES!

But, to avoid scrolling down the page, you can try using JavascriptExecutor. It should work even if the elements is not in view.

amitbobade
  • 500
  • 1
  • 4
  • 15
0

To clean up some confusion about 'visibility', in general it's not at all necessary for an element to be 'scrolled into view', i.e. within the bounds of an open window or frame that a human being can see. Not unless the elements in question have custom listeners, or use the Page Visibility API (see https://stackoverflow.com/a/1060034/954442) to control their visibility to human eyes.

After all, your page can be rendered 'offscreen', in-memory, by headless browsers, by PhantomJS, Slimer etc. You would still hope and expect your tests to work in this way, e.g. so you can run them continuously, over-night etc.

But as others have correctly said, many operations will fail with ElementNotVisibleException if the element in question has no size, or has been explicitly marked-up as not being visible, or indeed has been covered by another element.

Community
  • 1
  • 1
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73