Reputation: 45
I'm trying to get a list of the li on this website http://www.supremenewyork.com/shop/ but it doesn't do what I want it to do.
this is what I'm currently using
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());
}
}
and all it returns is
new
Edit: I'm trying to get the ul in the div with the shop class
Upvotes: 2
Views: 505
Reputation: 1523
if you want to print the ul inner html (or get its attributes such as id and style), you should do like:
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
}
In fact, there is a "new" text in the span of each li tag. That's why the method getText() prints "new".
Upvotes: 1
Reputation: 45
Thanks to everyone who helped me find a solution. here is my final code for anyone else that comes across this problem.
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;
}
}
Upvotes: 1
Reputation: 193088
If you observe the HTML of the website http://www.supremenewyork.com/shop/
the data you are looking for i.e. skate, jackets, sweatshirts, etc are contained as class
attribute of the <li>
tags. To get a list of the <li>
tags you can use the following code block :
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"););
}
Upvotes: 1