本文介绍了使用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)中向上或向下滚动页面
发布评论