简体   繁体   English

Selenium Webdriver跳过表中的行

[英]Selenium Webdriver skips rows in table

I am using HTMLUnitDriver for data extraction. 我正在使用HTMLUnitDriver进行数据提取。 I have a table with rows which is spread on more pages. 我有一个表,其中行分布在更多页面上。 Like for 50 results, page 1 contains 24 results, page 2 also contains 24 results and page 3 contains 2 results, which all sumed up make 50 results. 像50个结果一样,第1页包含24个结果,第2页包含24个结果,第3页包含2个结果,所有这些相加得出50个结果。 The problem that I have is that when I try to take all the rows from a page, I might not get all (24 rows), I could get any number less than 24, which I don't want. 我遇到的问题是,当我尝试获取页面中的所有行时,我可能不会得到全部(24行),我会得到小于24的任何数字,这是我不想要的。 I always want 24 rows, except for the last page which has less than 24 rows. 我总是要24行,除了最后一页少于24行。

The rows in the table have the following form: 表中的行具有以下形式:

<tr id="row28984" class="line_1" onclick="selectRow(28984);Field.activate('qty28984')">
<tr id="row93700" class="line_2" onclick="selectRow(93700);Field.activate('qty93700')">

I used xpath for taking them: 我用xpath接收它们:

xpath1: //*[@id='resultTable']/tbody/tr
xpath2: /html/body/center/table/tbody/tr[3]/td/form/table[2]/tbody/tr

I also used cssSelector like this: 我也这样使用cssSelector:

webDriver.findElements(By.cssSelector(".line_1,.line_2")

but none of what I tried is working. 但我尝试的所有方法均无用。 I also tried to wait for the page to completly load: 我还试图等待页面完全加载:

webDriver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS)

and tried to wait for the rows to load but nothing changed. 并尝试等待行加载,但没有任何变化。

One thing that I think it might work is putting Thread.sleep(1000) but I don't want to do this because it's not a best practice in my case and I want to use something offered by Selenium Webdriver. 我认为可能会起作用的一件事是放置Thread.sleep(1000),但我不想这样做,因为这不是我的最佳做法,我想使用Selenium Webdriver提供的功能。

Is there a solution to my problem? 我的问题有解决方案吗?

try alternative to Thread.sleep - fluentWait : fluentWait function returns your found web element. 尝试替代Thread.sleep - fluentWait :fluentWait函数返回找到的Web元素。 From the documentation on fluentWait: An implementation of the Wait interface that may have its timeout and polling interval configured on the fly. 从fluentWait上的文档中:等待接口的实现,可以动态配置其超时和轮询间隔。 Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. 每个FluentWait实例都定义了等待条件的最长时间,以及检查条件的频率。 Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page. 此外,用户可以配置等待以在等待时忽略特定类型的异常,例如在页面上搜索元素时的NoSuchElementExceptions。 Basic idea of different wait types in selenium you can get here in details . 硒中不同等待类型的基本概念,您可以在此处详细了解

fluentWait function to use (call): 使用(调用)fluentWait函数:

 public WebElement fluentWait(final By locator) {
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(30, TimeUnit.SECONDS)
        // .pollingEvery(5, TimeUnit.SECONDS)
        .pollingEvery(1, TimeUnit.SECONDS)
        // .ignoring(NoSuchElementException.class);
        .ignoring(org.openqa.selenium.NoSuchElementException.class);

        WebElement foo = wait.until(
               new Function<WebDriver, WebElement>() {
            public WebElement apply(WebDriver driver) {
                return driver.findElement(locator);
            }
        }
        );
        return foo;
    }

NOTE: it is up for to adjust polling time interval. 注意:可以调整轮询时间间隔。

So for you case I would try the following: 因此,对于您而言,我将尝试以下操作:

String elem1Selector="tr[id*=\"row\"][class=\"line_1\"]";
String elemt2Selector="tr[id*=\"row\"][class=\"line_2\"]";

Approach 1 方法1

// next step is to apply fluent wait: //下一步是应用流畅的等待:

elem1=fluentWait(By.cssSelector(elem1Selector));
elem2=fluentWait(By.cssSelector(elem1Selector)); 

Approach 2 Also we can try to use here explicit wait mechanism which is implemented in this way: 方法2我们也可以尝试在此处使用以这种方式实现的显式等待机制:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

or 要么

wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));

projecting to your table it be like: 投影到您的桌子上就像:

wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(elem1Selector)));

Hope this helps you. 希望这对您有所帮助。

Did you try to wait till the rows count captured by selenium is 24 ? 您是否尝试过等到硒捕获的行数为24?

long startTime = System.currentTimeMillis();
    do  {
            int size=driver.findElements(By.xpath("//*[@id='resultTable']/tbody/tr")).size();
            if(size>24)
                break;
        }while(System.currentTimeMillis()-startTime<10000);

you can adjust the time for waiting in while statement. 您可以调整在while语句中等待的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM