如何让Selenium webdriver读取csv文件以执行操作。我有我的脚本将其加载到浏览器中并执行操作如何使其读取csv文件并将该操作替换为csv文件中的数据
答案 0 :(得分:0)
执行此操作的最佳方法是使用另一个专门进行此类解析的库,并将其提供给内存。关于此问题的一个很好的StackOverflow问题是here。
答案 1 :(得分:0)
请记住,WebDriver只是与浏览器进行交互。这是你写作文件IO的语言。我建议创建一个CSV类。由于csv只是一个文本文件,因此您可以将其作为字符串读取并遍历每一行。用逗号分开,你很高兴。你没有指定一种语言,所以这里是我们库中C#的一个例子。注意我使用yield来提高效率。这是我在某处找到的东西。无需重新发明轮子。
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;
}
}
答案 2 :(得分: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));
快乐编码:)
答案 3 :(得分:0)
$(function(){
$('select').change( function(){
var value = $(this).val();
if (value == 'new') {
var other = prompt('Add new:');
if(!other) return false;
$(this).append('<option value"'
+ other
+ '" selected="selected">'
+ other
+ '</option>');
}
});
});