[英]Java Selenium. 2 column array. 2 elements to fill out on page. 1 loop per row
我正在搜索,但正在努力寻找正确的措辞来获得结果。
我有一个包含 2 列的 .csv 文件。 我需要导航到一个页面,sendKeys 列 1 到元素 1,sendKeys 列 2 到元素 2,单击元素 3,单击元素 4,循环下一行,直到数组中不再存在行。
除了“sendKeys column 1 to element 1, sendKeys column 2 to element 2”,我什么都想通了。
我有这个:
Scanner scan = new Scanner(new File("FilePathway"));
ArrayList<String[]> records = new ArrayList<String[]>();
String[] record = new String[2];
while (scan.hasNext()) {
record = scan.nextLine().split(",");
records.add(record);
}
// now records has your records.
// here is a way to loop through the records (process)
for (String[] temp : records) {
for (String temp1 : temp) {
System.out.print(temp1 + " ");
}
System.out.print("\n");
}
就像将数组打印到控制台的魅力一样。 很确定:
for (String[] temp : records) {
for (String temp1 : temp) {
driver.findElement(By.id("Element1")).sendKeys(temp1);
driver.findElement(By.id("Element2")).click();
driver.findElement(By.id("Element3")).click();
driver.findElement(By.id("Element4")).click();
}
如果我只有一列和一个元素要填写,那会起作用,但是我如何在其中获取第 2 列和第 2 个元素?
如果我正确理解你的问题,我认为这就是你要找的。
Scanner scan = new Scanner(new File("FilePathway"));
ArrayList<String[]> records = new ArrayList<String[]>();
String[] line = new String[2];
while (scan.hasNext()) {
line = scan.nextLine().split(",");
records.add(line);
}
// now records has your records.
// here is a way to loop through the records (process)
for (String[] record : records) {
driver.findElement(By.id("Element1")).sendKeys(record[0]);
driver.findElement(By.id("Element2")).sendKeys(record[1]);
driver.findElement(By.id("Element3")).click();
driver.findElement(By.id("Element4")).click();
}
我更改了一些变量名以使内容更好读... record
到line
, temp
到record
,然后删除了内部循环并将其替换为record[0]
和record[1]
。 line
只是 CSV 文件中的一行, record
是 records 中的单个records
, record[]
是一种使用数组表示法访问单个记录的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.