Selenium webdriver can't click on a link outside the page

0
0

I am having an issue with Selenium WebDriver. I try to click on a link that is outside the window page (you’d need to scroll up to see it). My current code is fairly standard:

menuItem = driver.findElement(By.id("MTP"));
menuItem
.click();
// I also tried menuItem.sendKeys(Keys.RETURN);

I know I could scroll up, and it would work in this case. But in a case where you have a long list of items, you don’t necessarily know how far you have to scroll down.

Is there any way to click on a link that is not on the visible part of the page (but that would be visible if you scroll)?

As a side note, I’m using Firefox, but I am planning to use IE7/8/9 and Chrome as well.

Any help would be greatly appreciated.

Edit: I’m afraid I can’t give the source code, as the company I work for doesn’t allow it, but I can give the code of the link I want to click on:

<div class="submenu">
<div id="MTP">Link title</div>
</div>

The exact same code works when the link is visible, only when it is not does it not work.

Edit2: Actually, oddly enough, it doesn’t raise any exception and just goes to the next instruction. So basically, what happens is:

menuItem = driver.findElement(By.id("MTP")); // no exception
menuItem
.click(); // no exception
//... some code ensuring we got to the next page: timeout reached
driver
.findElement(By.id("smLH")).click(); // NoSuchElementException, as we're on the wrong page.
  • You must to post comments
0
0

It is actually possible to scroll automatically to element. Although this is not a good solution in this case (there must be a way to get it working without scrolling) I will post it as a workaround. I hope someone will come up with better idea…

public void scrollAndClick(By by)
{
WebElement element = driver.findElement(by);
int elementPosition = element.getLocation().getY();
String js = String.format("window.scroll(0, %s)", elementPosition);
((JavascriptExecutor)driver).executeScript(js);
element
.click();
}
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website