如何让 Selenium webdriver 读取 csv 文件以执行操作。我有我的脚本可以在浏览器中加载它并执行一个操作我如何让它读取 csv 文件并将该操作替换为 csv 文件中的数据
问问题
13257 次
4 回答
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;
}
}
于 2013-08-27T17:43:04.657 回答
0
最好的方法是使用另一个专门从事这种解析的库,并将其输入内存。关于这个的一个很好的 StackOverflow 问题是here。
于 2013-08-26T18:53:44.283 回答
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;
}
}
我只是在我的代码中任何我想要的地方调用这些方法。
举个例子
WebElement username = driver.findElement(By.id("username"));
username.sendKeys(Utility.getSheetData("Login.xls",0,1,0));
快乐编码:)
于 2013-08-28T09:50:54.857 回答
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();
于 2015-06-25T13:05:38.013 回答