Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用SeleniumWebDriver在滚动动态加载网格中搜索元素?_Java_Ajax_Selenium_Selenium Webdriver - Fatal编程技术网

Java 如何使用SeleniumWebDriver在滚动动态加载网格中搜索元素?

Java 如何使用SeleniumWebDriver在滚动动态加载网格中搜索元素?,java,ajax,selenium,selenium-webdriver,Java,Ajax,Selenium,Selenium Webdriver,有一个网格,它有1000行,列名为Username(具有不同的值) 网格在每个视图中仅显示20行,其他行仅在滚动时加载 所以,如何在网格中搜索特定的用户名,因为只有元素被加载到scroll上 Scrollintoview方法有帮助吗?或者我需要使用window.scrollby()直到找到搜索的项目吗?我会随时使用“ScrollintoView”。 它将同时滚动和检查元素,并在查看元素时停止 要获得代码方面的帮助,请遵循首先,我道歉,因为我以前从未在网格上工作过。我认为这将是一个框架,更容易切换

有一个网格,它有1000行,列名为Username(具有不同的值)

网格在每个视图中仅显示20行,其他行仅在滚动时加载

所以,如何在网格中搜索特定的用户名,因为只有元素被加载到scroll上

Scrollintoview
方法有帮助吗?或者我需要使用
window.scrollby()
直到找到搜索的项目吗?

我会随时使用“ScrollintoView”。
它将同时滚动和检查元素,并在查看元素时停止

要获得代码方面的帮助,请遵循

首先,我道歉,因为我以前从未在网格上工作过。我认为这将是一个框架,更容易切换,然后使用JavascriptExecutor滚动到元素。但是,唉!对于网格,情况并非如此
而且,当涉及网格时,必须有一个表。

现在,这就是我的工作
  • 首先单击网格上的任何可见元素以使其聚焦。
  • 然后使用网格的定位器(xpath、id等)和“Keys.PAGE_DOWN”滚动网格,直到找到要查找的元素。
  • 如果在每次滚动中都找不到该元素,请处理它引发的异常并再次滚动。

    注意:不要忘记在每次滚动后给一些睡眠时间。

    我已经自动化了一个示例网格,并在下面附上了示例工作代码。希望这有助于解决问题:

    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());
            }
        }
    
    }
    
    import java.io.IOException;
    导入org.openqa.selenium.By;
    导入org.openqa.selenium.JavascriptExecutor;
    导入org.openqa.selenium.Keys;
    导入org.openqa.selenium.WebDriver;
    导入org.openqa.selenium.WebElement;
    导入org.openqa.selenium.firefox.FirefoxDriver;
    公共类滚动网格{
    公共静态void main(字符串[]args)引发IOException、InterruptedException{
    WebDriver=newfirefoxdriver();
    驱动程序。获取(“https://demos.devexpress.com/ASPxGridViewDemos/PagingAndScrolling/VirtualPaging.aspx");
    driver.manage().window().maximize();
    //单击网格内的元素以使其聚焦
    driver.findElement(By.xpath(“//*[@id='ContentHolder\u ASPxGridView1\u DXMainTable']///td[.='9/30/1994']))。单击();
    WebElement ele=null;
    int标志=0;
    整数计数=0;
    做{
    试一试{
    //在网格中滚动时要搜索的元素
    ele=driver.findElement(By.xpath(“//*[@id='ContentHolder\u ASPxGridView1\u DXMainTable']///td[.='3/28/1996']);
    flag=1;
    }捕获(可丢弃的e){
    //使用网格的xpath滚动网格
    findElement(By.xpath(“//*[@id='ContentHolder\u ASPxGridView1']///div[2]”);
    睡眠(3000);
    }
    }而((flag==0)| |(++count==250));
    如果(标志==1){
    System.out.println(“已找到元素!!”;
    }否则{
    System.out.println(“未找到元素!!”;
    }
    highlightElement(驱动程序,元素);//用于高亮显示元素
    Thread.sleep(5000L);//检查滚动到的元素是否高亮显示。
    driver.close();
    }
    //用于突出显示滚动后要定位的元素
    公共静态void highlightElement(WebDriver驱动程序,WebElement ele){
    尝试
    {
    对于(int i=0;i<3;i++)
    {
    JavascriptExecutor js=(JavascriptExecutor)驱动程序;
    js.executeScript(“参数[0].setAttribute('style',参数[1]);”,ele,“颜色:红色;边框:2px纯红;”;
    }
    }
    捕获(可丢弃的t)
    {
    System.err.println(“出现错误:+t.getMessage());
    }
    }
    }
    

    注意:现在可以正常工作了。如果元素被找到,或者在250次滚动后没有找到,它将退出循环。”250'是一个相对数。您可以将其更改为要在网格上执行的滚动次数。

    它如何与网格内的滚动条一起工作?网格必须位于框架内。因此,首先需要使用
    driver.switchTo().frame(//index或“//name或id of frame”或“Webelement in frame”)切换到框架
    ,然后,您可以使用scrollIntoView方法滚动到该元素。如果该元素不在iframe内或表内,该怎么办?请共享网格的HTML代码片段,或至少共享您尝试自动执行网格的网页链接。那么就很容易推断出这个问题我已经编辑了我的问题,我正在寻找一些poc/方法,以了解哪种方法最适合这种情况scenario@Vignesh,我已经修复了上面的卷轴计数代码。希望这能更好地帮助你。这个答案提供了一些关于如何处理此类情况的见解!!选中此项: