【问题标题】:Copy a text from excel and paste it in textbox and Click the link after Search从excel复制文本并将其粘贴到文本框中,然后单击搜索后的链接
【发布时间】:2015-01-10 03:04:55
【问题描述】:

我现在一直在与 Selenium 和 Sikuli 斗争​​。 目前我被困在我必须从 Excel 中获取值并在文本框中搜索它的地方(文本框在网页上,这是一个安全站点,即嵌入了 html 元素并且在页面源中不可见)。 搜索后,我将得到结果,如果出现结果,我将不得不单击链接,否则退出并从 excel 中搜索下一个值。

谁能帮我解决一下。 提前致谢

【问题讨论】:

  • 到目前为止您尝试了哪些方法,哪些方法不起作用?不要期望使用 Selenium 从 excel 中复制值,查找一些从 Java 中读取 CSV 文件的方法以开始使用
  • 嗨史蒂夫..我正在使用 apache-poi 从 excel 中读取数据

标签: java selenium selenium-webdriver sikuli


【解决方案1】:

你可以使用Apache POI从excel中读取一个单元格的值,试试这个:

首先,如果您使用 maven,请将其添加到您的 pom.xml:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>

然后使用此代码从 excel 文件中获取字符串值:

public static String ReadExcel (String filename) throws InvalidFormatException {

    File excel = null;
    FileInputStream file = null;

    try {

        excel = new File("D:\\tmp\\" + filename + ".xls");
        file  = new FileInputStream(excel);

        Workbook workbook = WorkbookFactory.create(file);
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {

            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {

                cell = cellIterator.next();

                if (!cell.getStringCellValue().isEmpty()) {


                    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {

                        // your method to find data on page with param cell.getStringCellValue()
                    }
                }
            }
        }

    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {

        if (file != null && excel != null) {

            file.close();
            //excel.delete();
        }
    }

    return null;
}

然后在每次单元格迭代时调用 search 方法,它将值与您在页面上需要的值进行比较。

希望对你有所帮助。

【讨论】:

  • 或者您可以将值放入列表中,例如,然后使用列表。
  • 谢谢 Nikita,但它不起作用。我必须根据另一列过滤 excel 列,然后将这些过滤后的值粘贴到网页上。
【解决方案2】:

您最好先解析 excel 文件然后验证值。

【讨论】:

    猜你喜欢
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2022-07-02
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    相关资源
    最近更新 更多