Selenium webdriver can't click on a link outsi

2020-01-25 07:09发布

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.

10条回答
Fickle 薄情
2楼-- · 2020-01-25 07:42

I used the method below to solve a similar issue for Selenium Java:

public static void scrollToElementByElement(WebElement element) {

    Coordinates coordinates = ((Locatable)element).getCoordinates();
    coordinates.inViewPort();
    coordinates.click(); //if needed

}

Then called on the method on my main test class

查看更多
女痞
3楼-- · 2020-01-25 07:49

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();
}
查看更多
三岁会撩人
4楼-- · 2020-01-25 07:49

It might be occurring because your header element or the footer element might be blocking the view of the element you want to perform action on. Selenium tries to scroll to the element position when it has to perform some action on the element (I am using Selenium WebDriver v3.4.0).

Here is a workaround -

private WebElement scrollToElementByOffset(WebElement element, int offset) {
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    jse.executeScript("window.scrollTo(" + element.getLocation().getX() + "," + (element.getLocation().getY()
            + offset) + ");");

    return element;
}

The above function scrolls the view to the element and then scrolls further by the offset you provide. And you can call this method by doing something like -

WebElement webElement = driver.findElement(By.id("element1"));
scrollToElementByOffset(webElement, -200).click();

Now, this is just a workaround. I gladly welcome better solutions to this issue.

查看更多
何必那么认真
5楼-- · 2020-01-25 07:52

I ran into a similar problem recently when there was a list of selectable objects in a JS dialog. Sometimes selenium would not select the correct object in the list. So i found this javascript suggestion:

WebElement target = driver.findElement(By.id("myId"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", target);
Thread.sleep(500); //not sure why the sleep was needed, but it was needed or it wouldnt work :(
target.click();

That solved my issue

查看更多
地球回转人心会变
6楼-- · 2020-01-25 07:52

Hey you can use this for ruby

variable.element.location_once_scrolled_into_view

Store the element to find in variable

查看更多
Melony?
7楼-- · 2020-01-25 07:52

Just an addition: In my case the button was overlapped by another floating button.

Just resizing the browser window solved the problem!

查看更多
登录 后发表回答