Baburaj V
Baburaj V

Reputation: 61

How to print the contents in the "ui","li" tags using selenium web driver

I am a newbee to selenium tool. I have a website with url : https://m.karmaloop.com/

I need to Print the contents in the Shop Mens Page like , Categories,Top Brands, New etc.

steps:
=====
1.Launch https://m.karmaloop.com/
2.Tap on Shop Mens image
3.User will be navigated to Shop Men's Page.
4.Fetch and display the contents like , Categories,Top Brands, New etc.

I have written the code for accessing it.But unfortunately it did not work.

Here is my code :

driver2.get(configuration.Home_url);

        driver2.findElement(By.xpath(configuration.Shopmens)).click();

        WebElement Mens_Object=driver2.findElement(By.id("browse-new-product"));

        System.out.println("clicked");

        List<WebElement> lists=Mens_Object.findElements(By.tagName("li"));

        System.out.println("captured  ul");

        lists.get(3).click();

        List<WebElement>  ListElements =lists.get(3).findElements(By.tagName("a"));

        System.out.println(ListElements.get(2).getText());

Can any one please help me to solve this task.?

Upvotes: 0

Views: 15031

Answers (5)

Tanushree
Tanushree

Reputation: 41

This will surely help to get elements in the list..

int i = 0;
      List<WebElement> elementsList = seldriver.findElements((By.xpath("/html/body/div[2]/div/div/div/div/div[2]/div/div[1]/div[4]/ul/li")));
      for(WebElement checkBox:elementsList) {

            checkBox = elementsList.get(i);
            String value = checkBox.getText();
            System.out.println("Checkbox = " + value);
             System.out.println(" i =" + i);
             i=i+1;
        }

Upvotes: 1

Mahesh Reddy Atla
Mahesh Reddy Atla

Reputation: 529

The below code is in python language which worked for me

driver.find_element_by_class_name("category-men").click()
options=driver.find_element_by_class_name("accordion")
for option in options:
    print option.text

Upvotes: 0

Akbar
Akbar

Reputation: 1525

Use this code:-

List<WebElement> liElements = driver.findElements(By
                .xpath("//ul[@id='browse-new-product']/li"));

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

        for (int i = 1; i < liElements.size()+1; i++) {
            WebElement linkElement = driver
                    .findElement(By
                            .xpath("//ul[@id='browse-new-product']/li[" + i
                                    + "]/a"));

            System.out.println(linkElement.getText());
        }

Upvotes: 2

Anuragh27crony
Anuragh27crony

Reputation: 2957

I tried to run ur code and it threw error saying the element is not visible.

If you want to perform action on menu "lists.get(3).click();" then always Select the "Group" and Find the "Sub Group" and perform action. (Make sure the element is visible)

Here is sample code i tried on the same website(https://m.karmaloop.com/). Here i'm trying to check the Categories >> Limited Edition

WebDriver Driver=new FirefoxDriver();
Driver.get("https://m.karmaloop.com/");
Driver.findElement(By.className("category-men")).click();
Driver.findElement(By.linkText("CATEGORIES")).click(); //Selecting a Group
Driver.findElement(By.linkText("LIMITED EDITION")).click(); //Now Selecting a sub Group

Other than playing with Tags, try to play with ID,LinkText,ClassName. Hope this helps

Upvotes: 0

Arpan Buch
Arpan Buch

Reputation: 1400

I am not sure this might work or not but please have a look

List<WebElement> allElements = driver.findElements(By.xpath("//div[@id='...']/ul/li")); 

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

Upvotes: 0

Related Questions