如何获得所有“李”元素“ul”在Selenium WebDriver中的类 [英] How to get all "li" elements of "ul" class in Selenium WebDriver

查看:764
本文介绍了如何获得所有“李”元素“ul”在Selenium WebDriver中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  public static void main(String []] args){

WebDriver driver = new FirefoxDriver();

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

列表< WebElement> allElements = driver.findElements(By.xpath(html / body / div [10] / div / div [1] / div [3] / ul [1] / li [5]));
System.out.println(allElements); (WebElement元素:allElements){
System.out.println(element.getText());


element.click();




$ b $ p
$ b $ p $ b $ $ b

解决方案

细节不清楚,但看起来您正尝试打印页脚的行业部分中的链接。如果这是真的,这应该工作。

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

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

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.

解决方案

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

这篇关于如何获得所有“李”元素“ul”在Selenium WebDriver中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆