在 Selenium C# 中滚动某些像素

我目前正在尝试使用一定数量的像素在 selenium C# 中向下滚动。我怎样才能做到这一点?

我的上一个项目,我有一个使用 python 的工作产品,但这是我切换到 C# 以来遇到的唯一问题。我不知道如何按像素量滚动。

在Python中,我能够做到:

pyautogui.scroll(-132)

它将在屏幕上向下滚动 132 像素。

但是,我在 C# 中使用 selenium,但不知道如何在 C# 中执行此操作。

页面内滚动条的图片

这是滚动条的检查元素的图像


SMILET
浏览 94回答 3
3回答

郎朗坤

向下滚动到特定像素:IJavascriptExecutor 在当前选定的框架或窗口的上下文中异步执行 JavaScript。scrollBy(0,1000) -> 滚动到 1000pxIJavascriptExecutor js = (IJavascriptExecutor) driver;js.executeScript("window.scrollBy(0,1000)");

哔哔one

最有可能的是,您有一个可滚动(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);}注意:上面的代码片段未编译。

炎炎设计

这是我正在使用的。        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");            }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python