2013-08-26 90 views
0

如何讓Selenium webdriver讀取csv文件以執行操作。我有我的腳本加載它在瀏覽器中,並執行一個操作如何讓它讀取CSV文件,並用csv文件中的數據替換該操作如何讓Selenium webdriver讀取csv文件以執行操作

+0

你能後的腳本示例

WebElement username = driver.findElement(By.id("username")); username.sendKeys(Utility.getSheetData("Login.xls",0,1,0)); 

快樂? SO不是一個完整教程的地方 - 更多的資源可以幫助你解決你的問題。 – vroomfondel

+0

硒與硒無關。 Selenium不解析CSV文件。你編碼的語言具有這種能力。 – Arran

回答

0

這樣做的最好方法是使用另一個庫專門研究這種解析,並將其饋入內存。一個好的StackOverflow關於這個問題是here

0

請記住,WebDriver只是簡單地與瀏覽器交互。這就是你寫作IO的語言。我建議創建一個CSV類。由於csv只是一個文本文件,因此您可以將其作爲字符串讀取並遍歷每行。用逗號分開,你很好走。你沒有指定一種語言,所以這裏是我們庫中C#的例子。注意我使用收益來提高效率。這是我在某處找到的東西。沒有必要重新發明輪子。

public static class CSV 
{ 
    private static bool ignoreFirstLineDefault = false; 

    public static IEnumerable<IList<string>> FromFile(string fileName) 
    { 
     foreach (IList<string> item in FromFile(fileName, ignoreFirstLineDefault)) yield return item; 
    } 

    public static IEnumerable<IList<string>> FromFile(string fileName, bool ignoreFirstLine) 
    { 
     using (StreamReader rdr = new StreamReader(fileName)) 
     { 
      foreach (IList<string> item in FromReader(rdr, ignoreFirstLine)) yield return item; 
     } 
    } 

    public static IEnumerable<IList<string>> FromStream(Stream csv) 
    { 
     foreach (IList<string> item in FromStream(csv, ignoreFirstLineDefault)) yield return item; 
    } 

    public static IEnumerable<IList<string>> FromStream(Stream csv, bool ignoreFirstLine) 
    { 
     using (var rdr = new StreamReader(csv)) 
     { 
      foreach (IList<string> item in FromReader(rdr, ignoreFirstLine)) yield return item; 
     } 
    } 

    public static IEnumerable<IList<string>> FromReader(TextReader csv) 
    { 
     //Probably should have used TextReader instead of StreamReader 
     foreach (IList<string> item in FromReader(csv, ignoreFirstLineDefault)) yield return item; 
    } 

    public static IEnumerable<IList<string>> FromReader(TextReader csv, bool ignoreFirstLine) 
    { 
     if (ignoreFirstLine) csv.ReadLine(); 

     IList<string> result = new List<string>(); 

     StringBuilder curValue = new StringBuilder(); 
     char c; 
     c = (char)csv.Read(); 
     while (csv.Peek() != -1) 
     { 
      switch (c) 
      { 
       case ',': //empty field 
        result.Add(""); 
        c = (char)csv.Read(); 
        break; 
       case '"': //qualified text 
       case '\'': 
        char q = c; 
        c = (char)csv.Read(); 
        bool inQuotes = true; 
        while (inQuotes && csv.Peek() != -1) 
        { 
         if (c == q) 
         { 
          c = (char)csv.Read(); 
          if (c != q) 
           inQuotes = false; 
         } 

         if (inQuotes) 
         { 
          curValue.Append(c); 
          c = (char)csv.Read(); 
         } 
        } 
        result.Add(curValue.ToString()); 
        curValue = new StringBuilder(); 
        if (c == ',') c = (char)csv.Read(); // either ',', newline, or endofstream 
        break; 
       case '\n': //end of the record 
       case '\r': 
        //potential bug here depending on what your line breaks look like 
        if (result.Count > 0) // don't return empty records 
        { 
         yield return result; 
         result = new List<string>(); 
        } 
        c = (char)csv.Read(); 
        break; 
       default: //normal unqualified text 
        while (c != ',' && c != '\r' && c != '\n' && csv.Peek() != -1) 
        { 
         curValue.Append(c); 
         c = (char)csv.Read(); 
        } 
        result.Add(curValue.ToString()); 
        curValue = new StringBuilder(); 
        if (c == ',') c = (char)csv.Read(); //either ',', newline, or endofstream 
        break; 
      } 

     } 
     if (curValue.Length > 0) //potential bug: I don't want to skip on a empty column in the last record if a caller really expects it to be there 
      result.Add(curValue.ToString()); 
     if (result.Count > 0) 
      yield return result; 

    } 
} 
+0

請指明你在哪找到它。來源是關鍵。 – Arran

+0

我知道。由於作者花時間考慮引用的逗號,我希望我記得我在哪裏找到該解析器。無論如何,即使整個代碼並不是我全部做的,我還是認爲我會發布最後一堂課。 –

0

我建議你使用Excel文件而不是csv。在使用CSV時的限制是當你想在數據本身中使用分隔符','時,例如如果你有一個地址字段說Gyandeep Colony, B. No : - 1/5在這裏','讓你的代碼破壞。

我做的讀取數據的方法是使用我的工具代碼

public class ExcelHandler { 

public static String getSheetData(String path,String sheetName, int col , int row) { 
    Workbook workbook; 
    String data = null; 
    try { 
     workbook = Workbook.getWorkbook(new File(path)); 
     Sheet sheet = workbook.getSheet(sheetName); 
     data = sheet.getCell(row,col).getContents(); 

    } catch (BiffException e) { 

     e.printStackTrace(); 
    } catch (IOException e) { 

     e.printStackTrace(); 
    } 

    return data; 

} 


public static String getSheetData(String path, int sheetNo, int col , int row) { 
    Workbook workbook; 
    String data = null; 
    try { 
     workbook = Workbook.getWorkbook(new File(path)); 
     Sheet sheet = workbook.getSheet(sheetNo); 
     data = sheet.getCell(row,col).getContents(); 

    } catch (BiffException e) { 

     e.printStackTrace(); 
    } catch (IOException e) { 

     e.printStackTrace(); 
    } 

    return data; 

} 

} 

我只是把這些方法無論我想在我的代碼。

讓我們編碼:)

0
FileInputStream file = new FileInputStream(new File("C:\\testdata.xls")); 
    HSSFWorkbook workbook = new HSSFWorkbook(file); 

    HSSFSheet sheet = workbook.getSheetAt(0); 

for (int i=1; i <= sheet.getLastRowNum(); i++){ 

     String keyword = sheet.getRow(i).getCell(0).getStringCellValue(); 

     searchbox.sendKeys(keyword); 

     searchbox.submit();  

     driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS); 

} 

    workbook.close(); 
    file.close(); 
相關問題