【问题标题】:Scroll table and store data in the list using Selenium web driver使用 Selenium Web 驱动程序滚动表格并将数据存储在列表中
【发布时间】:2016-10-19 05:14:15
【问题描述】:

我正在做一个想要做的项目 1. 滚动网页。 2.链接存储在表中 3. 点击行中的链接

((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
List<WebElement> req = driver
              .findElements(By.xpath("//table[@class='forceRecordLayout uiVirtualDataGrid--default uiVirtualDataGrid forceVirtualGrid resizable-cols']//tr"));

int total_req = req.size();
System.out.println(total_req);

for (int i = 0; i < total_req; i++) {
  String reqToClick = req.get(i).getText();
  if (reqToClick.equalsIgnoreCase("UPC_ 762016_Product Test- 06-07-2016 0")) {
      Thread.sleep(3000);
      req.get(i).click();
      Thread.sleep(3000);
      break;
  }
}

上面是我将数据存储到列表中的代码,但页面没有向下滚动。列表说显示没有滚动的计数。请帮帮我。

【问题讨论】:

  • stackoverflow.com/questions/12293158/… 试试这里提供的选项。
  • 我想向下滚动页面。随着页面向下滚动,我想将值存储在列表中,然后从列表中单击表内的链接。
  • 当您一直滚动到页面底部时会发生什么?是否加载整个表格或页面是否卸载先前的元素?我不认为您可以将我们链接到此示例?

标签: java selenium selenium-webdriver


【解决方案1】:

这似乎对我有用:

            WebElement table = wait.until(presenceOfElementLocated(By.tagName("tbody")));
        int len = table.findElements(By.tagName("tr")).size();
        for (int index = 0; index < len; index++) {
            WebElement tr = table.findElements(By.tagName("tr")).get(index);
            //save information in row
            if (index == len - 1) {
                JavascriptExecutor jse = (JavascriptExecutor) driver;
                jse.executeScript("window.scrollBy(0,250)");
                len = table.findElements(By.tagName("tr")).size();

            }

        }

当您向下滚动时,循环的长度会发生变化,因此它会获取表格中的所有行

【讨论】:

    【解决方案2】:
    public void ClickLink(string link)
    {
        //scroll to table.
    
        Actions action = new Actions(driver);
        action.MoveToElement(driver.FindElement(By.Id("tableID"));
    
        //get elements in list
        List<WebElement> req = driver.findElements(By
            .xpath("//table[@class='forceRecordLayout uiVirtualDataGrid--defaultuiVirtualDataGrid forceVirtualGrid resizable-cols']//tr"));
    
        List<string> linkNamesList = new List<string>();
    
        //Click the required link.
        foreach(IWebElement element in req)
        {
          string linkName = element.text;
          if(linkName == link)
          {
            element.click();
            break;
          }
       }
    }
    

    这应该可行。

    谢谢,

    拉克什·劳特

    【讨论】:

    • 谢谢拉克什。我不想滚动到表格,表格数据随着页面滚动而加载。我想在滚动操作加载数据后将数据存储在列表中。
    【解决方案3】:

    这不会向下滚动页面,而是将 web 表中的所有链接放入列表中,然后单击您需要的链接。

    【讨论】:

    • 第一行OP的代码向下滚动到页面底部。
    猜你喜欢
    • 2014-04-06
    • 2017-05-26
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2017-09-03
    相关资源
    最近更新 更多