使用Apache POI-Selenium Web驱动程序将dynamicWeb元素写入XLSX

您好我正在Selenium Webdriver上工作,我有一个下拉的网站,并select下拉值,然后点击一个button加载整个页面。 一旦页面加载,然后必须使用xpath从网页中find文本。 我想将下拉文本写入到xlx文件中,并且我想将文本(从xpath中find)写入xlx。 这两个值都是dynamic的。 如何开始,任何代码可以帮助我。 没有使用maven和selenium我想要写入数据到excel文件下面是我想写入excel文件的元素的屏幕截图

现场

我已经写下来获取下拉值和xpath文本 – 要将StockScripPattern写入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); 

曾经尝试过类似的东西,使用Apache POI lib,也可以使用JExcel,但据我所知它有一些限制。

在我的网站上find这个场景: mylearnings.net

在这里添加相同的内容以保证完整性

场景:

  • 考虑一个Excel文件,其中包含一个列表,如脚本名,CostPrice,Quantity,Price,Valuation(Price Quantity),Profit(Valuation-(CostPrice Quantity))。
  • 需要每天更新不同股票的价格细胞,估值和利润计算基于此。
  • 以下是此场景的代码。 即使您没有获得scheme,代码也会随着注释更新,您将会了解如何使用HSSF API的不同function。

     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库到您的构buildpath。 链接: Apache POI