使用 Selenium WebDriver(又名 Selenium 2.0)向下滚动鼠标 - JAVA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25363023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mouse scroll down using Selenium WebDriver (a.k.a. Selenium 2.0) - JAVA
提问by Keerthana
I am trying to do a mouse scroll in my automation testing (selenium webdriver). My page have loads of data where it take time to load all the datum.
我正在尝试在我的自动化测试(selenium webdriver)中进行鼠标滚动。我的页面有大量数据,加载所有数据需要时间。
My Requirement: I have a consolidated table with set of datum, where those records are displayed from the set of values that get displayed in the bottom of my page.
我的要求:我有一个包含一组数据的合并表,其中显示的记录来自我页面底部显示的一组值。
I am validating whether both the values are equal, for that I need the page to be completely scrolled to evaluate the same.
我正在验证两个值是否相等,为此我需要完全滚动页面以评估相同的值。
I used the below code:
我使用了以下代码:
Javascript jse = (Javascript)driver;
jse.executescript("scroll(0, 9000)");
This doesn't help it scrolled only half of the datum so my test get fail.
这无助于它只滚动了一半的数据,所以我的测试失败了。
Suggestions please...
建议请...
回答by Sitam Jana
We can use JavascriptExecutorto achieve this. Following is an example which will scroll this page from top to bottom:
我们可以使用JavascriptExecutor来实现这一点。以下是从上到下滚动此页面的示例:
WebDriver driver = new ChromeDriver();
driver.get("http://stackoverflow.com/questions/25363023/mouse-scroll-down-using-selenium-webdriver-2-0-java");
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollTo(0,document.body.scrollHeight);");
To use above code please import below utilities:
要使用上述代码,请导入以下实用程序:
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
Lemme know if this helps!
让我知道这是否有帮助!
回答by Dinesh Reddy
// Mouse Scroll Down
// 鼠标向下滚动
JavascriptExecutor Scrool = (JavascriptExecutor) driver;
Scrool.executeScript("window.scrollBy(0,300)", "");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
You can find elements after Scrolling
您可以在滚动后找到元素
driver.findElement(By.xpath(""));
// Mouse Scroll up
// 鼠标向上滚动
JavascriptExecutor Scrool = (JavascriptExecutor) driver;
Scrool.executeScript("window.scrollBy(0,-300)", "");
//To use above code please import below utilities
//要使用上面的代码,请导入下面的实用程序
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
回答by ekostadinov
Another thing that worked for me was the browser accessibility and TABbutton. As you know by sending TAB you can navigate trough the page as far as you know how many times you need to do it to get where you want. Also you can try clicking Arrows Up/Down:
对我有用的另一件事是浏览器可访问性和TAB按钮。正如您所知,通过发送 TAB,您可以浏览页面,只要您知道需要执行多少次才能到达您想要的位置。您也可以尝试单击向上/向下箭头:
1.Element.SendKeys(OpenQA.Selenium.Keys.ArrowUp);
2. char u = '\uE013'; // ASCII code for ArrowUp
char d = '\u0x50'; // code for ArrowDown
Element.SendKeys(Convert.ToString(u));