Java Selenium 将 Web 元素列表中的项目添加到字符串数组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18475578/
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
Selenium adding items from a Web element list to a string array list
提问by vslat
I'm doing some automated testing and I'm trying to save some items from a web element list to a string array, there should be some parsing involved but I'm not sure how, see code snippet below
我正在做一些自动化测试,我正在尝试将 Web 元素列表中的一些项目保存到字符串数组中,应该涉及一些解析,但我不确定如何进行,请参阅下面的代码片段
public void I_should_see_the_following_folders(DataTable expectedData) throws Throwable {
int tbl1; int tbl2;
List<List<String>> featureTable = expectedData.raw();
WebElement folders = driver.findElement(By.id("folders"));
List <WebElement> emailFolders = folders.findElements(By.className("folder-name"));
List<String> foo = new ArrayList<String>();
for (List<String> featuresList : expectedData.raw())
foo.add(featuresList.get(0));
tbl1 = emailFolders.size();
tbl2 = featureTable.size();
List<String> webList = new ArrayList<String>();
for(int i=0;i<tbl1;i++){
webList.add(emailFolders.get(0));
}
}
what I'm trying to do is take a datatable list of items, convert it into a string array and then take a list of items from the webpage and also store it into a string array and then compare each array to determine if the elements are present and are the same in not particular order.
我想要做的是获取项目的数据表列表,将其转换为字符串数组,然后从网页中获取项目列表并将其存储到字符串数组中,然后比较每个数组以确定元素是否为存在并且没有特定的顺序相同。
I think I got the data table array list to work, but need some help with the web elements array list.
我想我让数据表数组列表起作用了,但需要一些关于 web 元素数组列表的帮助。
Thanks!!!
谢谢!!!
回答by Lingaraj R M
Please try using following sample code, change xpath/data accordingly
请尝试使用以下示例代码,相应地更改 xpath/data
List<WebElement> resultList = _driver.findElements(By.xpath("//img[contains(@src,'tab-close.png')]/preceding-sibling::span[@class='tab-name ng-binding']"));
for (WebElement resultItem : resultList){
String tabname=resultItem.getText();
if(resultItem.getText().equalsIgnoreCase("Dashboard")){
GlobalFunctions.clickOnButtonCustomised(false, _driver, By.xpath("//span[@class='tab-name ng-binding' and contains(text(),'"+tabname+"')]/following-sibling::img[contains(@src,'tab-close.png')]"), "");
}
}
回答by barak manos
Change this:
改变这个:
for (int i=0; i<tbl1; i++)
webList.add(emailFolders.get(0));
To this:
对此:
for (WebElement emailFolder : emailFolders)
webList.add(emailFolder.getAttribute(XXX));
Then, replace XXX
with the name of the attribute which stores the actual folder name.
然后,替换XXX
为存储实际文件夹名称的属性名称。
A few examples:
几个例子:
- For
<tag><class="folder-name" id="folder1"></tag>
, replaceXXX
with"id"
- For
<tag><class="folder-name" value="folder1"></tag>
, replaceXXX
with"value"
- For
<tag><class="folder-name">folder1</tag>
, replaceXXX
with"innerHTML"
- 对于
<tag><class="folder-name" id="folder1"></tag>
,替换XXX
为"id"
- 对于
<tag><class="folder-name" value="folder1"></tag>
,替换XXX
为"value"
- 对于
<tag><class="folder-name">folder1</tag>
,替换XXX
为"innerHTML"
BTW, instead of getAttribute("innerHTML")
, you can simply use getText()
.
顺便说一句,getAttribute("innerHTML")
您可以简单地使用getText()
.
回答by Chris Liston
There may be an easier way to convert a List of WebElements to String[] array, but this generic method works -
可能有一种更简单的方法将 WebElements 列表转换为 String[] 数组,但这种通用方法有效 -
// iterate through all elements adding to array we are returning
public static String[] listToStringArray(List<WebElement> inList) {
String[] outArray = new String[inList.size()]; // instantiate Array
for (int i = 0; i < inList.size(); i++) {
outArray[i] = inList.get(i).getText();
}
}
return outArray;
}