When using Selenium 2.0, the .click() method has the capability to automatically scroll until the element is visible and can be clicked on, as shown in the code snippet below:
WebElement box = driver.findElement( By.id( boxID ) );
box.click();
Generally, this feature works smoothly as Selenium will scroll until the box is visible and then perform the click operation.
However, there might be instances where it fails due to an org.openqa.selenium.WebDriverException when an element with a higher z-index is present. In such cases, the element gets scrolled to but remains invisible due to a lower z-index, such as a navigation bar at the top of the page.
One possible workaround is to use JavaScript to scroll to the top of the page, causing the element to appear at the bottom. Although this solution may not be ideal or recommended...
JavascriptExecutor jse = (JavascriptExecutor)driver;
// scrolling to the top will move the box to the bottom of the page
jse.executeScript("scroll(0, -10000);");
Is there a more elegant way to handle this issue throughout an entire test suite without resorting to a workaround like JavaScript scrolling?