Vig*_*vam 5 java ajax selenium selenium-webdriver
Grid中有1000行,其中一列名为Username(具有不同的值).
并且网格每个视图只显示20行,其他行仅在滚动时加载(ajax).
那么,如何在网格中搜索特定的用户名,因为我们只有滚动时加载的元素.
请问Scrollintoview方法的帮助?或者我需要使用,window.scrollby()直到找到搜索到的项目?
首先,我很抱歉,因为我以前从未在网格上工作过。我认为这将是一个框架,并且使用JavascriptExecutor可以更轻松地切换然后滚动到该元素。可惜!网格不是这种情况。
并且,当涉及网格时,必须有一个表。
现在,这对我有用。
注意:每次滚动后,请不要忘记提供一些睡眠时间。
我已经自动化了一个示例网格,并在下面附加了示例工作代码。希望这有助于解决问题:
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:这现在可以正常工作。如果找到该元素,或者在250次滚动后找不到,它将退出循环。“ 250”是相对数字。您可以将其更改为要在网格上执行的滚动数。
| 归档时间: |
|
| 查看次数: |
10388 次 |
| 最近记录: |