1

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.

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
user3168935
  • 13
  • 1
  • 8
  • http://stackoverflow.com/questions/12293158/page-scroll-up-or-down-in-selenium-webdriver-selenium-2-using-java Try the option provided here. – Sujay Jun 17 '16 at 11:45
  • I wanted to scroll the page down. As page scrolled down,I wanted to store the values in the list and from the list I wanted click on the link inside table. – user3168935 Jun 17 '16 at 12:07
  • What happens when you scroll all the way to the bottom of the page? Does the entire table load or does the page unload the previous elements? I don't suppose you can link us to an example of this? – JeffC Jun 29 '16 at 19:29

3 Answers3

1

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

saeedeh
  • 89
  • 1
  • 13
0

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.

SaiPawan
  • 1,189
  • 7
  • 20
0
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

Rakesh Raut
  • 183
  • 3
  • 12
  • Thanks Rakesh. I dont wants to scroll to the table, table data getting loaded as page getting scrolled. I wanted to store data in the list after it gets loaded on the scroll action. – user3168935 Jun 17 '16 at 12:43