使用java在Selenium WebDriver(Selenium 2)中向上或向下滚动页面

编程入门 行业动态 更新时间:2024-10-10 23:25:19
本文介绍了使用java在Selenium WebDriver(Selenium 2)中向上或向下滚动页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 Selenium 1(又名 Selenium RC)中编写了以下代码,用于使用 java 进行页面滚动:

I have written the following code in Selenium 1 (a.k.a Selenium RC) for page scrolling using java:

selenium.getEval("scrollBy(0, 250)");

Selenium 2 (WebDriver) 中的等效代码是什么?

What is the equivalent code in Selenium 2 (WebDriver)?

推荐答案

场景/测试步骤:1. 打开浏览器并导航到 TestURL2.向下滚动一些像素并向上滚动

Scenario/Test steps: 1. Open a browser and navigate to TestURL 2. Scroll down some pixel and scroll up

对于向下滚动:

WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("window.scrollBy(0,250)");

或者,您可以执行以下操作:

OR, you can do as follows:

jse.executeScript("scroll(0, 250);");

对于向上滚动:

jse.executeScript("window.scrollBy(0,-250)"); OR, jse.executeScript("scroll(0, -250);");

滚动到页面底部:

场景/测试步骤:1. 打开浏览器并导航到 TestURL2. 滚动到页面底部

Scenario/Test steps: 1. Open a browser and navigate to TestURL 2. Scroll to the bottom of the page

方式 1:使用 JavaScriptExecutor

jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");

方式 2:按 ctrl+end

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.END);

方式 3:使用 Java Robot 类

Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_END); robot.keyRelease(KeyEvent.VK_END); robot.keyRelease(KeyEvent.VK_CONTROL);

更多推荐

使用java在Selenium WebDriver(Selenium 2)中向上或向下滚动页面

本文发布于:2023-11-27 16:25:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   java   Selenium   WebDriver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!