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 standa
Instead of move the scrollbar to the button position, which sometimes it didn't work for me, I send the enter key to the button
var element = driver.FindElement(By.Id("button"));
element.SendKeys(Keys.Enter);
This solution worked like a charm for me:
public void click(By by) throws Exception{
WebElement element = driver.findElement(by);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
element.click();
}
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
This works for me (in c#)-
item = driver.findelement(by.....);
item.SendKeys(Keys.LeftControl);
item.Click();
Just an addition: In my case the button was overlapped by another floating button.
Just resizing the browser window solved the problem!
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