使用selenium java获取li的列表

时间:2018-04-04 17:42:24

标签: java selenium

我正在尝试在此网站http://www.supremenewyork.com/shop/上获取li的列表,但它并没有按照我的意愿去做。

这就是我目前使用的

Public Function JoinStringCollection(colStrings As Collection, strDelimiter As String) As String
'This function joins a collection with a delimiter so that there is no need to lop off a trailing or leading delimiter
    Dim strOut As String
    Dim i As Long

    If colStrings.Count > 0 Then
        strOut = colStrings.Item(1)
    End If

    If colStrings.Count > 1 Then
        For i = 2 To colStrings.Count
            strOut = strOut & strDelimiter & colStrings.Item(i)
        Next
    End If

    JoinStringCollection = strOut
End Function

并且它返回的全部是

public static void main(String[] args) throws Exception {

    System.setProperty("webdriver.chrome.driver", "Your\\ChromeDriver\\Path");

    ChromeOptions options = new ChromeOptions();
    options.setHeadless(false);

    WebDriver driver = new ChromeDriver(options);

    driver.get("http://www.supremenewyork.com/shop/");

    List<WebElement> allElements = driver.findElements(By.cssSelector("ul#shop-scroller")); 

    System.out.println(allElements.size());

    for(WebElement e : allElements) {
        System.out.println(e.getText());
    }

}

编辑:我正在尝试使用商店类

获取div中的ul

3 个答案:

答案 0 :(得分:1)

如果你想打印ul inner html(或获取它的属性,如id和style),你应该这样做:

  driver.get("http://www.supremenewyork.com/shop/");
            List<WebElement> allElements = driver.findElements(By.cssSelector("ul#shop-scroller"));
   WebElement ul = allElements.get(0);

   // getting ul data
   System.out.println("ul id = " + ul.getAttribute("id"));
   System.out.println("ul style = " + ul.getAttribute("style"));
   System.out.println("inner html: ");
   System.out.println(ul.getAttribute("innerHTML"));


   // getting all li in ul
   List<WebElement> liList = ul.findElements(By.tagName("li"));
   for(WebElement li: liList) {
       System.out.println("li class = " + li.getAttribute("class"));
       System.out.println("li style = " + li.getAttribute("style"));
       System.out.println("inner html: ");
       System.out.println(li.getAttribute("innerHTML"));
       System.out.println("---------");
      // getting the image  in li
     List<WebElement> imgList = li.findElements(By.tagName("img"));
        WebElement img = imgList.get(0);                
       // do something

}

事实上,有一个&#34; new&#34;每个li标签范围内的文本。这就是方法getText()打印&#34; new&#34;。

的原因

答案 1 :(得分:1)

如果您观察到网站http://www.supremenewyork.com/shop/ HTML 您要查找的数据,即滑冰夹克运动衫等包含在class标签的<li>属性中。要获取<li>标记的列表,您可以使用以下代码块:

List<WebElement> allElements = driver.findElements(By.cssSelector("ul#shop-scroller li")); 
System.out.println(allElements.size());
for(WebElement e : allElements) {
    System.out.println(e.getAttribute("class"););
}

答案 2 :(得分:1)

感谢所有帮助我找到解决方案的人。这是我遇到这个问题的其他人的最终代码。

public static void main(String[] args) throws Exception {

    System.setProperty("webdriver.chrome.driver", "C:\\Users\\Name\\Desktop\\WebStuff\\chromedriver.exe");

    ChromeOptions options = new ChromeOptions();
    options.setHeadless(false);

    WebDriver driver = new ChromeDriver(options);

    driver.get("http://www.supremenewyork.com/shop/");

    List<WebElement> allElements = driver.findElements(By.cssSelector("ul#shop-scroller")); 

    WebElement ul = allElements.get(0);

    List<WebElement> liList = ul.findElements(By.tagName("li"));

     for(WebElement li: liList) {
    /*       System.out.println("li class = " + li.getAttribute("class"));
           System.out.println("li style = " + li.getAttribute("style"));
           System.out.println("inner html: ");
           System.out.println(li.getAttribute("innerHTML"));
           System.out.println("---------");
           */
           WebElement link = li.findElement(By.tagName("a"));

           System.out.println(link.getAttribute("href"));

           driver.get(link.getAttribute("href"));

           return;

       }



}