使用Apache POI-Selenium Web驱动程序将动态Web元素编写到XLSX

时间:2015-07-19 06:10:47

标签: java excel selenium xpath dynamic

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

Site

我写过以获取下拉值和xpath文本 - 想要将 StockScrip 模式写入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);

1 个答案:

答案 0 :(得分:0)

如果尝试使用Apache POI lib做了类似的东西,你也可以使用JExcel,但据我所知,它几乎没有限制。

在我的网站上找到该方案:mylearnings.net

此处添加相同内容以确保完整性:

情景:

  • 考虑一个Excel文件,其中的工作表包含诸如scriptname,CostPrice,Quantity,Price,Valuation(Price Quantity),Profit(Valuation-(CostPrice Quantity))等列。
  • 需要每天为不同的股票更新价格单元格,并根据此计算估值和利润。
  • 以下是此方案的代码。即使您没有获得方案,也会使用注释更新代码,以便了解如何使用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