【问题标题】:How to read data from excel file one line at a time如何一次从excel文件中读取一行数据
【发布时间】:2014-09-21 20:20:26
【问题描述】:

我希望我的程序从 excel 文件中读取一行(将包含一个 URL)。转到该 URL 并阅读下一行 --> 转到该 URL 直到读取整个文件。

import java.io.*;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class readDataFromExcelFile{

 public static void main(String[] args) { 

        WebDriver driver = new FirefoxDriver();

        driver.get("http://www.google.com");

        driver.manage().window().maximize();  

        WebElement searchbox = driver.findElement(By.name("q"));

 try {

  FileInputStream file = new FileInputStream(new File("E://TestData.xlsx")); 
  XSSFWorkbook workbook = new XSSFWorkbook(file);

  XSSFSheet sheet = workbook.getSheetAt(0);

for (int i=1; i <= sheet.getLastRowNum(); i++){

        String keyword = sheet.getRow(i).getCell(0).getStringCellValue();

        searchbox.sendKeys(keyword);

        searchbox.submit();       

        driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);

}

  file.close();

 } catch (FileNotFoundException fnfe) {
  fnfe.printStackTrace();
 } catch (IOException ioe) {
  ioe.printStackTrace();
 }
 }
}

但是上面的程序会一次性获取所有的/url。你能告诉我我应该在代码中修改什么吗?

【问题讨论】:

  • 代码看起来正确。请更具体地说明“一次所有网址”的含义。它是否搜索url1url2url3?还是在搜索之间根本没有等待?
  • 你好,它接受为 url1url2url3 最终它不会是一个有效的 url

标签: java excel automation


【解决方案1】:

在输入更多文本之前清除该字段。

sendKeys() 就像它所说的那样:它向浏览器发送一系列“用户按下的键 X”。因此,如果该字段已包含内容,则附加新键。

见:Clear text from textarea with selenium

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 2011-08-15
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多