如何搜索的动态加载网格使用webdriver的硒元素卷轴? [英] How to search for element in dynamic loading grid on scroll using selenium webdriver?

查看:171
本文介绍了如何搜索的动态加载网格使用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.

现在,这是很适合我。

  • 首先点击网格任何可见的元素,让它成为关注的焦点。
  • 在使用Keys.PAGE_DOWN'直到你找到你正在寻找的元素,然后滚动使用网格定位器网格(的XPath,身份证等)。
  • 在情况下,该元素不是在每个卷轴发现,除了处理它引发异常并再次滚动。

    Now, this is what has worked for me.

  • First click on any visible element on grid to get it into focus.
  • Then scroll the grid using grid's locator(xpath,id,etc.) using 'Keys.PAGE_DOWN' till you find the element you are looking for.
  • In case the element is not found on each scrolls, than handle the exception it raises and scroll again.

    注意:不要忘了给每个滚动经过一段睡眠时间

    我有自动化一个样本格,并附加了样品工作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.

    这篇关于如何搜索的动态加载网格使用webdriver的硒元素卷轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆