Java 如何在 Selenium WebDriver 中获取“ul”类的所有“li”元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32497998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:38:44  来源:igfitidea点击:

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

javahtmlseleniumselenium-webdriver

提问by Tarun

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.

我是 Selenium webdriver 的新手。我遇到了一个要求,我必须运行我的测试,点击一个部分中的所有链接。有人可以帮我编写 Java 代码吗?附上一张显示该特定部分的萤火虫属性的图像。我试过下面的代码,但它返回一个空列表。

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.

提前致谢。

回答by Bartosz Drzewinski

You could use like for example:

例如,您可以使用 like:

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.

使用附加到 html 根目录的 XPath 通常是个坏主意,即绝对 xpath,您应该始终尝试使用最短的选择器,即相对 xpath。

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

还请记住,如果链接被隐藏,您需要触发操作来启用它们 - 例如打开菜单。

回答by Sravan547

You can try with the below code.

您可以尝试使用以下代码。

Just change the xpath according to your application.

只需根据您的应用程序更改 xpath。

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());      

        }

回答by JeffC

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...

您无法单击此循环中的链接,因为单击第一个链接后,您将不再位于该页面上。我建议您将每个链接的 URL 存储在一个数组中,然后driver.get(url)为每个链接存储。或者,您可以将预期的 URL 存储在一个数组中,并将链接中的 URL 与预期的 URL 进行比较,而根本不必导航。这个选择由你...

回答by david_c

The solution from JeffC works with the tweak detailed below -

JeffC 的解决方案适用于下面详述的调整 -

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.

将导致 IndexOutOfBoundsException。

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

要修复,只需将迭代器设置为从 0 而不是 1 开始