selenium scroll to element c#
var element = driver.FindElement(By.id("element-id"));
Actions actions = new Actions(driver);
actions.MoveToElement(element);
actions.Perform();
selenium scroll to element c#
var element = driver.FindElement(By.id("element-id"));
Actions actions = new Actions(driver);
actions.MoveToElement(element);
actions.Perform();
how to scroll in selenium
Selenium does not have a method for scrolling
but there are some ways to scroll:
#1 ->=moveToElement= coming from Actions class
will scroll down and up to given web element
#2 Using JSExecutor: We can inject JavaScript
code in our Java+Selenium code using JSExecutor
which helps us scroll up, down, left, right.
We need to create instance of JS executor,
then cast our driver type of it.
==================================================
syntax is =
JavaScriptExecutor js = (JavaScriptExecutor) Driver.getDriver();
js.executeScript("in here we need to pass js code that scrolls");
js.executeScript("window.scrollBy(0,250)");
js.executeScript("arguments[0].scrollIntoView(true);", WebElement);
selenium scroll to element c#
public void ScrollTo(int xPosition = 0, int yPosition = 0)
{
var js = String.Format("window.scrollTo({0}, {1})", xPosition, yPosition);
JavaScriptExecutor.ExecuteScript(js);
}
public IWebElement ScrollToView(By selector)
{
var element = WebDriver.FindElement(selector);
ScrollToView(element);
return element;
}
public void ScrollToView(IWebElement element)
{
if (element.Location.Y > 200)
{
ScrollTo(0, element.Location.Y - 100); // Make sure element is in the view but below the top navigation pane
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us