Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. 
like image 320
Stilltorik Avatar asked Aug 20 '12 09:08

Stilltorik


People also ask

Why click is not working in Selenium?

We can list the most common reasons for click problems as being one of the following: Wrong web element locations. The existence of a web element that obscures the web element that we want to click. The Selenium WebDriver works much faster than the response of the application.

How do I click on a specific link in Selenium?

New Selenium IDE A hyperlink on a page is identified with the anchor tag. To click a link, we can use the link text locator which matches the text enclosed within the anchor tag. We can also use the partial link text locator which matches the text enclosed within the anchor tag partially.

What is xOffset and yOffset in Selenium?

Both xOffset and yOffset represent the relative pixel distance (integer) with which to scroll. For xOffset , positive value means to scroll right, and negative value means to scroll left. For yOffset , positive value means to scroll downward, and negative value means to scroll upward.


1 Answers

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(); } 
like image 130
JacekM Avatar answered Sep 26 '22 01:09

JacekM