eclipse 在 Seleniumwebdriver 中读取文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32113349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading text file in Seleniumwebdriver
提问by Test
I want to read values from a text file using selenium webdriver. I want to get third text from the text file. The text file is something like this. 1.Apple 2.Orange 3.Grape I want to read the third option(Grape) and to display it. Please help
我想使用 selenium webdriver 从文本文件中读取值。我想从文本文件中获取第三个文本。文本文件是这样的。1.Apple 2.Orange 3.Grape 我想阅读第三个选项(Grape)并显示它。请帮忙
回答by bob
If you are able to read the text file and store data in a String, then you can use some regular expressionto get the third option.
如果您能够读取文本文件并将数据存储在 String 中,那么您可以使用一些正则表达式来获得第三个选项。
String ps = ".*3.([A-Za-z]*)"; // regex
String s = "1.Apple 2.Orange 3.Grape"; // file data in a String object
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(s);
if (m.find()){
System.out.println(m.group(0)); // returns value of s
System.out.println(m.group(1)); // returns result= Grape
}
Check regex here : https://regex101.com/r/cF9pB7/1
在此处检查正则表达式:https: //regex101.com/r/cF9pB7/1
You can get some other values by changing the regulaar
您可以通过更改正则来获得其他一些值
回答by Mayur Shah
You do not need selenium to text file. Selenium is just a browser automation tool. You can use below code to read text file using Java.
您不需要硒到文本文件。Selenium 只是一个浏览器自动化工具。您可以使用以下代码使用 Java 读取文本文件。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\testing.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
You can integrate it with your selenium code.
您可以将其与硒代码集成。