2015-07-19 42 views
1

嗨我正在使用Selenium Webdriver,我的網站上有一個下拉列表,並且必須選擇下拉值,然後單擊一個按鈕以加載整個頁面。一旦頁面加載,然後必須使用xpath從網頁中找到文本。我想將下拉文本寫入xlx文件,並且我想將文本(從xpath發現)寫入xlx。這兩個值都是動態的。如何開始,任何代碼可以幫助我。 不使用Maven和硒我想將數據寫入到excel文件 下面是我想寫到Excel中元素的截屏文件使用Apache POI-Selenium網絡驅動程序將動態Web元素寫入XLSX

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); 

回答

0

已經嘗試了類似的東西回來,使用Apache POI lib,你也可以使用JExcel,但據我所知它有一些限制。 mylearnings.net

這裏添加相同內容的完整性:

在我的網站找到場景

場景:

  • 考慮具有片狀與像列的Excel文件,腳本名稱,CostPrice,數量,價格,估價(價格數量),利潤(估價 - (成本價格數量))。
  • 需要爲不同的股票每天更新價格單元格,估值和利潤根據此計算。
  • 以下是此場景的代碼。即使您沒有獲得方案,代碼也會隨着註釋更新,以便您瞭解如何使用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

+0

嗨卡萬,感謝您的快速回復。我想先創建一個新文件,然後將下拉和xpath文本的動態數據推送到excel文件。這是我的第一個對象。更新表單將在稍後完成。我想推送到 – Bhaskar

+0

的數據,我會留給你弄清楚。閱讀Apache POI文檔。 – Kavan