3 回答
TA贡献1921条经验 获得超9个赞
向下滚动到特定像素:IJavascriptExecutor 在当前选定的框架或窗口的上下文中异步执行 JavaScript。scrollBy(0,1000) -> 滚动到 1000px
IJavascriptExecutor js = (IJavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
TA贡献1854条经验 获得超8个赞
最有可能的是,您有一个可滚动(div)元素,您可以将其作为参数(可滚动)传递给下面的函数。
public static void scrollDownByPixAmount(IWebElement scrollable, int pixelsToScroll)
{ //assuming your driver var is a static member of same class
(driver as IJavaScriptExecutor).ExecuteScript($"arguments[0].scrollTop = {pixelsToScroll};", scrollable);
}
注意:上面的代码片段未编译。
TA贡献1808条经验 获得超4个赞
这是我正在使用的。
public static void scrollDownByPixAmount(string value)
{
var windowScroll = string.Format("window.scrollBy(0,{0})", value);
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript(windowScroll, "");
}
public static void scrollUpByPixAmount(string value)
{
var value1 = "-" + value;
var windowScroll = string.Format("window.scrollBy(0,{0})", value1);
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript(windowScroll, "");
}
public static void scrollToElement(string value)
{
IWebElement val = driver.FindElement(By.XPath(value));
Actions actions = new Actions(driver);
actions.MoveToElement(val);
actions.Perform();
scrollDownByPixAmount("150");
}
添加回答
举报