首页 >Java >java教程 >如何使用 WebDriver (Java) 在 Selenium 2 中滚动页面?

如何使用 WebDriver (Java) 在 Selenium 2 中滚动页面?

Susan Sarandon
Susan Sarandon原创
2024-11-10 02:02:021053浏览

How to Scroll Pages in Selenium 2 with WebDriver (Java)?

使用 WebDriver (Java) 在 Selenium 2 中进行页面滚动

在 Selenium 1 中,页面滚动是使用 selenium.getEval("scrollBy(0, 250)" 实现的) 方法。在 Selenium 2 中,等效代码如下:

向下或向上滚动

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;

// Scroll down
jse.executeScript("window.scrollBy(0,250)");
// OR
jse.executeScript("scroll(0, 250);");

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

滚动到页面底部

使用JavaScriptExecutor:

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

使用 Keys.CONTROL 键.END:

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

使用 Java 机器人类:

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

以上是如何使用 WebDriver (Java) 在 Selenium 2 中滚动页面?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn