Hello everyone, I am currently learning about Selenium Automation and have created a website as shown below. Here is the HTML code:
<!DOCTYPE html>
<html>
<head><title>Shop</title></head>
<body>
<ul>
<li><a href="www.rendertofruitsection.com">Fruits</a>
<ul>
<li><a href="www.addbananatocart.com">» Banana</a></li>
<li><a href="www.addappletocart.com">» Apple</a></li>
<!--List goes on-->
</ul>
</li>
<li><a href="www.redertovegsection">Vegetable</a>
<ul>
<li><a href="www.addcapcicumtocart">» Capsicum</a></li>
<li><a href="www.addtomatotocart.com">» Tomato</a></li>
<li><a href="www.addoniontocart">» Onion</a></li>
<!--List goes on-->
</ul>
</li>
<li><a herf="www.nonveglink.com">Non Veg Section</a></li>
<!--List goes on-->
</ul>
</body>
</html>
Initially, my goal was to extract all the text inside the <a>
tag using Java Selenium. Here is the Java code I used:
public class shop {
public static void main(String[] args) {
WebDriver driver= new ChromeDriver();
driver.get("www.myshopping.com");
List<WebElement> text = driver.findElements(By.xpath("//a"));
for(int i=0; i<text.size(); i++) {
System.out.println(i + ". " + text.get(i).getText());
}
}
}
The output I received did not include the nested <ul>
tags and their <li>
items. Can someone provide me with a hint on how to achieve this in Selenium Java?
I attempted some solutions but got stuck at this point. Any help or guidance would be greatly appreciated. Thank you!