java 如何在实现 selenium webdriver 时将表中的 WebElements 列表存储到列表中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14634126/
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-10-31 16:54:22  来源:igfitidea点击:

How to store list of WebElements from a table in to a list while implementing selenium webdriver?

javalistselenium-webdriverui-automation

提问by user2030364

I am trying to save a column of elements into a list from a table structure with the below expression on which I need to perform a click operation to validate those buttons.

我正在尝试使用以下表达式将一列元素从表结构保存到列表中,我需要对其执行单击操作以验证这些按钮。

Code :

代码 :

I have the value for Total_element = 37.

我有Total_element = 37.

for(int start=0; start <= Total_element; start++)  
{  
    int startn=start+1;  
    System.out.println(start);  
    List <WebElement> Element1 = new ArrayList<WebElement>() ;

    try{  
        Element1.add(Naveen.findElement(By.xpath(".//*[@id='data_grid']/tbody/tr["+startn+"]/td[2]/a/img")));  
    }catch(Throwable t){  
        System.out.println(t);  
    }  
    System.out.println(Element1.get(start));  
    System.out.println("The element" + start + "is :"+ Element1.get(start));  
    Naveen.findElement(By.xpath(Element1.get(start).toString())).click();  
    Naveen.findElement(By.xpath(".//*[@id='action']/a/span/div")).click();  
    System.out.println("The element" + start + "is :"+ Element1);  
    Thread.sleep(5000);  
}

Error :

错误 :

when I try to retrieve the elements from the list I get the following output :

当我尝试从列表中检索元素时,我得到以下输出:

[[FirefoxDriver: firefox on XP (586a8f1f-f784-4ae7-adf5-5f920dfad8e0)] -> xpath: .//*[@id='data_grid']/tbody/tr[1]/td[2]/a/img]

[[FirefoxDriver: firefox on XP (586a8f1f-f784-4ae7-adf5-5f920dfad8e0)] -> xpath: .//*[@id='data_grid']/tbody/tr[1]/td[2]/a/img]

further which my is saying my operation is failing.

此外,我说我的操作失败了。

回答by Manigandan

Actually what is happening mean, the return type of the

实际上发生的事情意味着,

   driver.findElemnt(By.xpath("xpath"));

is the WebElement. While you adding the above code to the ArrayListit will add the WebElement. The WebElement contains the information about the

是 WebElement。当您将上述代码添加到ArrayList它时,它将添加 WebElement。WebElement 包含有关

Driver Used- FirefoxDriver
Browser session value- 586a8f1f-f784-4ae7-adf5-5f920dfad8e0
locator used- xpath: .//*[@id='data_grid']/tbody/tr[1]/td[2]/a/img]

Driver Used- FirefoxDriver
Browser session value- 586a8f1f-f784-4ae7-adf5-5f920dfad8e0
locator used- xpath: .//*[@id='data_grid']/tbody/tr[1]/td[2]/a/img]

If you trying to retrieve the Web Element it will return all those things. That is what happening in your case and you are getting error while trying to click.

如果您尝试检索 Web 元素,它将返回所有这些内容。这就是您的情况,您在尝试单击时遇到错误。

You can just add the Xpath locatoralone in the ArrayList. It will work.

您可以只Xpath locatorArrayList. 它会起作用。

Try this

试试这个

 ArrayList<String> Element1 = new ArrayList<String>();
 Element1.add(".//*[@id='data_grid']/tbody/tr["+startn+"]/td[2]/a/img");

 driver.findElement(By.xpath(Element1.get(`startn`))).click();

回答by Sneh Tripathi

Instead of:

代替:

Naveen.findElement(By.xpath(Element1.get(start).toString())).click();

try:

尝试:

Element1.get(start).click();