【问题标题】:Writing dynamic web elements to XLSX using Apache POI-Selenium web driver使用 Apache POI-Selenium Web 驱动程序将动态 Web 元素写入 XLSX
【发布时间】:2015-10-08 12:19:28
【问题描述】:

您好,我正在开发 Selenium Webdriver,我有一个带有下拉菜单的网站,必须选择下拉值,然后单击一个按钮来加载整个页面。页面加载后,必须使用 xpath 从网页中查找文本。我想将下拉文本写入 xlx 文件,并且我想将文本(从 xpath 中找到)写入 xlx。这两个值都是动态的。如何开始,任何代码都可以帮助我。 不使用 maven 和 selenium 我想将数据写入 excel 文件 下面是我要写入excel文件的元素的屏幕截图

我已经写了获取下拉值和 xpath 文本- 想将StockScrip和Pattern写入excel文件


import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import ExcelData.BullishBearishExcelFile;

public class Driver {
public static  WebDriver driver;
public  static  void main(String[] args) {
BullishBearishExcelFile data = new BullishBearishExcelFile();
driver= new FirefoxDriver();
driver.get("http://www.icharts.in");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30000, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a/img[@src='http://www.icharts.in/StockGlance.png']")).click();
//driver.manage().timeouts().implicitlyWait(30000, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver,30);
WebElement webelescripDropDown = driver.findElement(By.id("symbol"));
Select stockName= new Select(webelescripDropDown);
List<WebElement> stocks = stockName.getOptions();
int stockCount = stocks.size(); 
for(int j=1;j<=stockCount;j++){
webelescripDropDown = driver.findElement(By.id("symbol"));
stockName= new Select(webelescripDropDown);
stockName.selectByIndex(j);
String stockScrip = stockName.getOptions().get(j).getText();
System.out.println(stockScrip+"=stockname clicked");
driver.findElement(By.id("action")).click();
WebElement pattern = driver.findElement(By.xpath("//*[contains(text(),'Short  Term (5 days) :')]"));
String bullishPattern = pattern.getText();
System.out.println("Pattern is ="+bullishPattern);

【问题讨论】:

    标签: java excel selenium xpath dynamic


    【解决方案1】:

    很久以前就尝试过类似的东西,使用 Apache POI lib,你也可以使用 JExcel,但据我所知它几乎没有限制。

    在我的网站上找到场景:mylearnings.net

    为了完整起见,在此处添加相同的内容:

    场景:

    • 考虑一个 Excel 文件,其中包含一个表格,其中包含脚本名称、CostPrice、Quantity、Price 、Valuation(PriceQuantity)、Profit(Valuation-(CostPriceQuantity)) 等列。
    • 需要每天更新不同股票的价格单元,并据此计算估值和利润。
    • 以下是此场景的代码。即使您没有得到场景,代码也会使用 cmets 更新,因此您将了解如何使用 HSSF API 的不同功能。

      public static void writeToExcelHSSF(String filename)
      {
      try{
          //Create a workbook, as here we are trying to modify an Existing file need to provide FileInputStream as argument               
          HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(path + xlsFile));
      
          //FileOutputStream will be needed while writing changes back to a xls file
          FileOutputStream stream = new FileOutputStream(path + "final1.xls");
      
          //Get sheet by providing index of the sheet
          HSSFSheet sheet = wb.getSheetAt(0);
      
          //Creating a evaluator, it will be used to evaluate formula of a cell
          HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb); 
      
          //Get numder of rows of a sheet 
          int rows = sheet.getPhysicalNumberOfRows();
          System.out.println("Sheet " + wb.getNumberOfSheets() + " \"" + wb.getSheetName(0) + "\" has " + rows    + " row(s).");
      
          //Create a row object;
          HSSFRow row;
      
          //Different cell objects
          HSSFCell scriptCell;
          HSSFCell priceCell;
          HSSFCell valuationCell;
          HSSFCell profitCell;
          String scriptName;
      
          for(int k=1;k< rows;k++){
      
                      //get row based on index(row number)    
                      row = sheet.getRow(k);
      
                      //get particular cell of a row                  
                      scriptCell = row.getCell(9);
      
                      //get string value from the cell
                      scriptName =scriptCell.getStringCellValue();
      
                      priceCell =row.getCell(5);
                      valuationCell =row.getCell(6);
                      profitCell =row.getCell(7);
      
                      //set value in a cell, (here parseStocks is just a user defined function to fetch value of a stock)
                      priceCell.setCellValue(parseStocks(path + filename,scriptName));
      
                      //Trigger cache clearance, so evaluation of formula on this cell is done with latest value
                      evaluator.notifyUpdateCell(priceCell);
      
                      //Re-evaluate formula of a cell
                      evaluator.evaluateFormulaCell(valuationCell);
                      evaluator.evaluateFormulaCell(profitCell);
          }
      
          //writing back workbook to output stream
          wb.write(stream);
          stream.close();
      }
      catch( IOException e)
      {
      e.printStackTrace();
      }   
      }
      

    不要忘记将 apache POI 库添加到您的构建路径。 链接:Apache POI

    【讨论】:

    • 嗨,Kavan,感谢您的快速回复。我想先创建一个新文件,然后将动态数据从下拉列表和 xpath 文本推送到 excel 文件。这是我的第一个对象。稍后会更新工作表。我要推送到的数据
    • 嗯,我会留给你自己弄清楚。浏览 Apache POI 文档。
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2015-09-19
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    相关资源
    最近更新 更多