簡體   English   中英

Java Selenium。2 列數組。 要在頁面上填寫的 2 個元素。 每行 1 個循環

[英]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();
}

我更改了一些變量名以使內容更好讀... recordlinetemprecord ,然后刪除了內部循環並將其替換為record[0]record[1] line只是 CSV 文件中的一行, record是 records 中的單個recordsrecord[]是一種使用數組表示法訪問單個記錄的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM