[英]How to store global Array of multiple items from a list for later use
I have my selenium code which pulls a list of names available in the list. 我有我的selenium代码,它列出了列表中可用的名称列表。 Now I want to store the same names as global for later use.
现在我想存储与全局相同的名称以供以后使用。 Please help.
请帮忙。
I have tried array process, it pulls only true with each line, but not the values. 我已经尝试过数组进程,它只对每行提取,但不是值。
List<WebElement> allText = driver.findElements(By.xpath("//*[@id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"));
int total = allText.size();
System.out.println(total);
for(int i=3;i<=total;i++)
{
CaselevelSigningCMs =driver.findElement(By.xpath("//*[@id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"+"["+i+"]"+"/td[2]")).getText();
System.out.println(CaselevelSigningCMs);
}
I should get the names like: Ranjit Nyk, Sudhanva G.... I have to verify those names in other pages in other class/method. 我应该得到如下名字:Ranjit Nyk,Sudhanva G ....我必须在其他类/方法的其他页面中验证这些名称。 CaselevelSigningCMs is a global variable, it pulls single item only.
CaselevelSigningCMs是一个全局变量,它只提取单个项目。 I need similar array defined as global so that it can pull multiple items.
我需要将类似的数组定义为全局,以便它可以提取多个项目。
CaselevelSigningCMs =driver.findElement(By.xpath("//*[@id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"+"["+i+"]"+"/td[2]")).getText();
See how this String CaselevelSigningCMs is defined. 了解如何定义String CaselevelSigningCMs。 Just like that go for ArrayList
就像那样去ArrayList
Lets say the string used is defined some where like 让我们说使用的字符串是在某些地方定义的
public String CaselevelSigningCMs = "";
Similarly define ArrayList 类似地定义ArrayList
public List<String> collectedItems=new ArrayList<>();
in loop add get text of each element, something like 在循环中添加获取每个元素的文本,类似于
collectedItems.add(driver.findElement(By.xpath("//*[@id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"+"["+i+"]"+"/td[2]")).getText());
You can use Arraylist to store and retrieve them
ArrayList ts=new ArrayList(); ArrayList ts = new ArrayList();
store the objects in arraylist 将对象存储在arraylist中
ts.add(); ts.add();
You can use Stream.map() function in order to convert the list of WebElements to the List of Strings containing element text attribute in a single shot: 您可以使用Stream.map()函数将WebElements列表转换为单个镜头中包含元素文本属性的字符串列表:
List<String> allText = driver.findElements(By.xpath("//*[@id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"))
.stream()
.map(WebElement::getText)
.collect(Collectors.toList());
allText.forEach(System.out::println);
I also don't really like your XPath expression, especially: 我也不太喜欢你的XPath表达式,特别是:
//*
wildcard //*
通配符 table[2]
and tr[3]
table[2]
和tr[3]
wildcard expressions take longer time to execute and consume more resources, and your approach seems to be very dependent on DOM structure so I would recommend using XPath Axes as well as XPath Functions / Operators to make your expression as relative, robust and reliable as possible. 通配符表达式需要更长的时间来执行和消耗更多的资源,并且您的方法似乎非常依赖于DOM结构,所以我建议使用XPath Axes以及XPath函数/运算符来使表达式尽可能相对,健壮和可靠。
You can also consider using Table class of the HtmlElements framework for working with HTML tables 您还可以考虑使用HtmlElements框架的Table类来处理HTML 表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.