c# - 在 Selenium 中将页面几乎滚动到末尾

标签 c# selenium

我需要在 Selenium 中滚动页面几乎到页面末尾(150 像素到底部)。但是我的代码不起作用。它滚动到底部。它是如何修复的?

IWebElement element = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("javascript:window.scrollBy(0,document.body.scrollHeight-150)");

最佳答案

试试这个:

((IJavaScriptExecutor)driver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight - 150)");

一些注意事项:

  • 您正在执行滚动命令,它不会返回 IWebElement,因此不需要 IWebElement element = 部分。
  • 你也不需要 javascript: 部分
  • 因为你想滚动到一个绝对位置,scrollTo更合适

关于c# - 在 Selenium 中将页面几乎滚动到末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18833064/

相关文章:

ruby-on-rails - capybara :无法在 Mac 上使用 selenium 驱动程序截取屏幕截图

c# - 如何在 C# 中获取应用程序的安装日期

c# - 直接显卡访问

c# - 如何同时触发客户端和 MVC 验证?

c# - 我的应用程序和 Access 查询向导之间的不同 LIKE 行为

r - 树莓派上的 Selenium/standalone-firefox docker 无法工作 : how to use RSelenium on a raspberryPi

c# - UWP 显示全屏弹出窗口、ContentDialog 或 Flyout

java - 类似于WebDriverWait,仅适用于WebElement

Python selenium send_keys 在 Chrome 中不起作用

java - selenium 3.3.1 中的 Actions 类已弃用,使用 contextClick 寻找解决方案