4

I'm trying to access the name of different products displayed on a website using selenium. For example on https://www.supremenewyork.com/shop/all/jackets i'm able to locate the products (webElements) and put them in a list but I can't get their name (as displayed under the image). Is there a way to do this using Selenium (in java)?

I have tried most of the methods in the WebElement Interface's API.

driver.get("https://www.supremenewyork.com/shop/all/");
ArrayList <WebElement> list = new ArrayList<>();
list.addAll(driver.findElements(By.className("inner-article")));
for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i).getTagName());
}

i would expect a console output displaying the names of all the product of the page However i get a list of the same string : "div".

CGK
  • 2,662
  • 21
  • 24
xszn
  • 127
  • 5

3 Answers3

1

You have to use .getText(). replace the line to

System.out.println(list.get(i).getText());
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • If you feel the issue is resolved, please accept the answer by clicking on the hallow check mark below the down vote button on the left hand side. Feel free to upvote :-) – supputuri Jul 05 '19 at 19:23
0

Selenium docs state that
"Get the tag name of this element. Not the value of the name attribute: will return "input" for the element ."

If you are trying to get the name attribute. You should try

System.out.println(list.get(i).getAttribute("name"));

Name can be changed for any other attribute that you want.

Akin Okegbile
  • 1,108
  • 19
  • 36
0

You could try using getText() method, according to documentation it will return all of visible text from the element and all subelements. If you need only product name or color, try using By.ByCssSelector(String cssSelector) and specify selector as ".inner-article p a" for color or ".inner-article h1 a" for name when selecting elements.

impune
  • 66
  • 4