How to get all "li" elements of "ul" class in Selenium WebDriver
Asked Answered
M

5

7

I'm new to Selenium webdriver. I came across a requirement where I have to run my test which clicks on all links with in a section. Can someone help me with the Java code for this. Attached a image which shows firebug properties of that particular section. I have tried the below code but it returns me a null list.

public static void main(String[] args) {

    WebDriver driver = new FirefoxDriver();

    driver.get("https://dev.www.tycois.com/");
    driver.manage().window().maximize();

    List<WebElement> allElements = driver.findElements(By.xpath("html/body/div[10]/div/div[1]/div[3]/ul[1]/li[5]"));
    System.out.println(allElements);

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

Thanks in advance.

Memberg answered 10/9, 2015 at 9:26 Comment(1)
Attachment is missing and please specify the operation you're trying to perform in details.Iridis
D
9

The details aren't clear but it looks like you are trying to print the links in the INDUSTRIES section of the footer. If that's true, this should work.

driver.get("https://dev.www.tycois.com/");
WebElement industries = driver.findElement(By.cssSelector("div.columns.three.alpha > ul"));
List<WebElement> links = industries.findElements(By.tagName("li"));
for (int i = 1; i < links.size(); i++)
{
    System.out.println(links.get(i).getText());
}

You can't click the links in this loop because once you click the first one, you will no longer be on the page. I would suggest that you store the URLs from each link in an array and then driver.get(url) for each one. Or you could store the expected URLs in an array and compare the URLs from the links to the expected URLs and not have to navigate at all. The choice is up to you...

Doby answered 1/10, 2015 at 0:50 Comment(0)
B
4

The solution from JeffC works with the tweak detailed below -

driver.get("https://dev.www.tycois.com/");
WebElement industries = 
driver.findElement(By.cssSelector("div.columns.three.alpha > ul"));
List<WebElement> links = industries.findElements(By.tagName("li"));
for (int i = 0; i < links.size(); i++)
{
    System.out.println(links.get(i).getText());
}

The alternate answer above, which I cannot comment on because I'm new to the site had

for(int i=1; i < links.size(); i++)

However this misses off the first element in the list. The suggested fix to use -

for(int i=1; i <= links.size(); i++)

will cause an IndexOutOfBoundsException.

To fix, simply set your iterator to start at 0 instead of 1

Brassie answered 20/6, 2018 at 14:51 Comment(0)
D
0

You could use like for example:

driver.findElements(By.xpath("//li[contains(@class,'managed-services')]/ul/li/a"));

It is usually bad idea to use XPath attached to root of html i.e Absolute xpath, you should always try to use the shortest selectors possible i.e Relative xpath.

Also remember that if links are hidden, you need to trigger action, which enables them - like open menu for instance.

Desiderate answered 10/9, 2015 at 10:16 Comment(0)
B
0

You can try with the below code.

Just change the xpath according to your application.

List<WebElement> liElements = driver.findElements(By.xpath(".//*[@id='fk-mainhead-id']/div[2]/div/div/ul/li"));
        System.out.println(liElements);

        for(int i=1; i <= liElements.size(); i++)
        {
            WebElement linkElement = driver.findElement(By.xpath(".//*[@id='fk-mainhead-id']/div[2]/div/div/ul/li[" + i + "]/a"));
            System.out.println(linkElement.getText());      

        }
Bernadinebernadotte answered 10/9, 2015 at 12:59 Comment(1)
As int i is initialized by 1, the condition should be <= instead of <Pelite
M
0
WebElement industries = driver.findElement(obj.getElement("history_list"));
List<WebElement> links = industries.findElements(By.tagName("li"));
boolean a = false;
Thread.sleep(10000);
for (WebElement option : links) {
    System.out.println("value =" + option.getText());
    String s = option.getText();
    if (s.equals(new_site_name)) {
        a = true;
        break;
    } else {
        continue;
    }
}
Misery answered 31/12, 2022 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.