如何搜索的动态加载网格使用webdriver的硒元素卷轴?
有格有说1000行与指定的列名(有不同的值)。
There is Grid which has say 1000 rows with a column named Username(with distinct values).
和网格将显示按次只有20行,而其他行会的加载(阿贾克斯)只在滚动。
And the grid will display only 20 rows per view, and the other rows will be loaded(ajax) only on scrolling.
那么,如何寻找在网格中特定的用户名,因为我们只有元素上滚动得到加载。
So, how to search for a particular username in the grid, since we have only elements getting loaded on scroll.
确实 Scrollintoview
办法帮助?或者我需要使用 window.scrollby()
,直到我找到搜索的项目?
Does Scrollintoview
method help? Or do i need to use window.scrollby()
until i find the searched item?
首先,我道歉,因为我以前从来没有在一个网格。我认为这将是一个框架,会更容易切换,然后用 JavascriptExecutor 滚动到的元素。但是,唉!这不是一格的情况。
而且,必须有参与网格当一个表。
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.
现在,这是很适合我。
Now, this is what has worked for me.
注意:不要忘了给每个滚动经过一段睡眠时间
我有自动化一个样本格,并附加了样品工作code以下。希望这有助于搞清楚的问题:
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());
}
}
}
注意:这正常工作了。它会在情况下,元素被发现,或者如果经过250卷轴没有发现循环出来。 '250'是一个相对数。你可以把它改成你要对电网进行滚动的次数。
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.