java 如何使用 selenium webdriver 在滚动时在动态加载网格中搜索元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26729532/
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
How to search for element in dynamic loading grid on scroll using selenium webdriver?
提问by Vignesh Paramasivam
There is Grid which has say 1000 rows with a column named Username(with distinct values).
有 Grid 有 1000 行,其中有一列名为 Username(具有不同的值)。
And the grid will display only 20 rows per view, and the other rows will be loaded(ajax) only on scrolling.
每个视图网格将仅显示 20 行,其他行将仅在滚动时加载(ajax)。
So, how to search for a particular username in the grid, since we have only elements getting loaded on scroll.
那么,如何在网格中搜索特定的用户名,因为我们只有在滚动时加载元素。
Does Scrollintoview
method help? Or do i need to use window.scrollby()
until i find the searched item?
请问Scrollintoview
方法的帮助?还是我需要使用window.scrollby()
直到找到搜索到的项目?
回答by Subh
First of all, I apologise because I had never worked on a grid before. I thought it will be a frame and will be easier to switch and then scroll to the element using JavascriptExecutor. But, alas! That's not the case for a grid.
And, there must be a table when a grid is involved.
首先,我很抱歉,因为我以前从未在网格上工作过。我认为它将是一个框架,并且会更容易切换,然后使用JavascriptExecutor滚动到元素。可惜!网格不是这种情况。
并且,当涉及到网格时,必须有一个表。
Now, this is what has worked for me.
现在,这对我有用。
Note: Do not forget to give some sleep time after each scroll.
I have automated one sample grid, and have attached the sample working code below. Hope this helps in figuring out problem:
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ScrollGrid{
public static void main(String[] args) throws IOException, InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.get("https://demos.devexpress.com/ASPxGridViewDemos/PagingAndScrolling/VirtualPaging.aspx");
driver.manage().window().maximize();
//Clicking on an element inside grid to get it into focus
driver.findElement(By.xpath("//*[@id='ContentHolder_ASPxGridView1_DXMainTable']//td[.='9/30/1994']")).click();
WebElement ele=null;
int flag=0;
int count=0;
do{
try{
//element to search for while scrolling in grid
ele = driver.findElement(By.xpath("//*[@id='ContentHolder_ASPxGridView1_DXMainTable']//td[.='3/28/1996']"));
flag=1;
} catch(Throwable e){
//scrolling the grid using the grid's xpath
driver.findElement(By.xpath("//*[@id='ContentHolder_ASPxGridView1']//div[2]")).sendKeys(Keys.PAGE_DOWN);
Thread.sleep(3000);
}
}while((flag==0) || ((++count)==250));
if(flag==1){
System.out.println("Element has been found.!!");
}else{
System.out.println("Element has not been found.!!");
}
highlightElement(driver, ele); //For highlighting the element
Thread.sleep(5000L); //to check if the element scrolled to is highlighted.
driver.close();
}
//For highlighting the element to be located after scroll
public static void highlightElement(WebDriver driver, WebElement ele) {
try
{
for (int i = 0; i < 3; i++)
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1]);",ele, "color: red; border: 2px solid red;");
}
}
catch(Throwable t)
{
System.err.println("Error came : " +t.getMessage());
}
}
}
Note:This works correctly now. It will come out of the loop in case the element is found, or if not found after 250 scrolls. '250' is a relative number. You can change it to the number of scrolls you want to perform on the grid.
注意:不要忘记在每次滚动后给一些睡眠时间。
我已经自动化了一个示例网格,并附上了下面的示例工作代码。希望这有助于找出问题:
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ScrollGrid{
public static void main(String[] args) throws IOException, InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.get("https://demos.devexpress.com/ASPxGridViewDemos/PagingAndScrolling/VirtualPaging.aspx");
driver.manage().window().maximize();
//Clicking on an element inside grid to get it into focus
driver.findElement(By.xpath("//*[@id='ContentHolder_ASPxGridView1_DXMainTable']//td[.='9/30/1994']")).click();
WebElement ele=null;
int flag=0;
int count=0;
do{
try{
//element to search for while scrolling in grid
ele = driver.findElement(By.xpath("//*[@id='ContentHolder_ASPxGridView1_DXMainTable']//td[.='3/28/1996']"));
flag=1;
} catch(Throwable e){
//scrolling the grid using the grid's xpath
driver.findElement(By.xpath("//*[@id='ContentHolder_ASPxGridView1']//div[2]")).sendKeys(Keys.PAGE_DOWN);
Thread.sleep(3000);
}
}while((flag==0) || ((++count)==250));
if(flag==1){
System.out.println("Element has been found.!!");
}else{
System.out.println("Element has not been found.!!");
}
highlightElement(driver, ele); //For highlighting the element
Thread.sleep(5000L); //to check if the element scrolled to is highlighted.
driver.close();
}
//For highlighting the element to be located after scroll
public static void highlightElement(WebDriver driver, WebElement ele) {
try
{
for (int i = 0; i < 3; i++)
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1]);",ele, "color: red; border: 2px solid red;");
}
}
catch(Throwable t)
{
System.err.println("Error came : " +t.getMessage());
}
}
}
注意:这现在可以正常工作。如果找到元素,或者在 250 次滚动后未找到,它将退出循环。“250”是一个相对数字。您可以将其更改为要在网格上执行的滚动次数。