简体   繁体   English

网页元素解析

[英]Webelement parsing

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. Tr-> 课程选择-> 课程编号。

    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

You can simply your code by locating course-selection-title directly and use it to locate the courses under it您可以通过直接定位course-selection-title来简化您的代码,并使用它来定位其下的课程

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']"));
}

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

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