0

So I was pulling information from this Website and it was running really slow. List<WebElement> listofprogramrequirements = row.findElements(By.className("course-selection-title")); I am assuming I need a single object and to pull my information from the row. Tr-> course-selection-> course-numbers.

    WebElement tableelement = 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("table")));
    List<WebElement> trs = tableelement.findElements(By.tagName("tr"));
    for (WebElement row : trs) 
    {
        List<WebElement> listofprogramrequirements = row.findElements(By.className("course-selection- 
        title"));
        for (WebElement col : listofprogramrequirements) 
        {
            System.out.println(col.getText());
            List<WebElement> coursenumbers = row.findElements(By.className("course-number")); 
            if(coursenumbers.size()>0)
            {
                Iterator<WebElement> iter = coursenumbers.iterator();
                // This will check whether list has some element or not
                while (iter.hasNext()) {
                    WebElement item = iter.next();
                    String label = item.getText();
                    System.out.println(label);
                }
            }
            else
            {
            }
        }
    }

  **Produces:**
  All of
  CPSC 1050
  CPSC 1150
  CPSC 1160
  CPSC 1181
  CPSC 2150
  MATH 2362
  Two of
  One of
  MATH 1171
3
  • could you elaborate your question?
    – Amruta
    Jan 23, 2020 at 3:55
  • My code runs incredibly slow for what it is supposed to do. Is there a way to rewrite this to increase the speed. I need to grab the course-select, course-numbers to write out what it produces above. After the first loop it goes slow. Jan 23, 2020 at 3:58
  • The website has a table which I need to grab the all the tr/td colspan="2", from there I grab the course-selection class name and then grab all the course-numbers while printing the info. Jan 23, 2020 at 4:08

1 Answer 1

0

You can simply your code by locating course-selection-title directly and use it to locate the courses under it

List<WebElement> listofprogramrequirements = driver.findElements(By.className("course-selection-title"));
for (WebElement col: listofprogramrequirements) {
    System.out.println(col.getText());
    List<WebElement> coursenumbers = col.findElements(By.xpath(".//..//td[@class='course-number']"));
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.