user3168935
user3168935

Reputation: 13

Scroll table and store data in the list using Selenium web driver

I'm working on a project where wanted to 1. Scroll the web page. 2. links are stored in the table 3. Click on the link present in the row

((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;
  }
}

Above is the code where I'm storing data into the list, but the page is not scrolling down. List says showing count without scroll. Please help me out.

Upvotes: 1

Views: 6795

Answers (3)

saeedeh
saeedeh

Reputation: 89

This seem to work for me:

            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();

            }

        }

the length of the loop changes when you scroll down so it gets all the rows in the table

Upvotes: 1

Rakesh Raut
Rakesh Raut

Reputation: 183

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;
      }
   }
}

This should work.

Thanks,

Rakesh Raut

Upvotes: 0

SaiPawan
SaiPawan

Reputation: 1194

This will not scroll down the page but take all the links in the web table into the list and click on the link which you need.

Upvotes: 0

Related Questions