我正在使用Selenium Java Webdriver。我想检查一下这个词" good"包含在页面中并存储在ArrayList中找到的每个内容。
例如。该页面包含单词" good"," goodmorning" " goodafternoon"。然后我应该得到一个ArrayList = [good,goodmorning,goodafternoon]。
我认为检查某些文字是否包含在"等页面中的传统方法包含("")"在这种情况下不会工作。
你们说什么?可以吗?
答案 0 :(得分:2)
这是您正在寻找的,它使用的是Selenium WebDriver。我在这个页面上测试了这个词" good"并得到了预期的结果。
public List<String> perform(String url, String searchWord) {
// webdriver that opens the given URL
driver.get(url);
searchWord = searchWord.toLowerCase();
// get the top most element on page, it will be html in most cases
WebElement html = driver.findElement(By.cssSelector("html"));
// gets all the text on page
String htmlText = html.getText().replaceAll("\n", " ").toLowerCase();
// split by space to get all words on page
String[] allWords = htmlText.split(" ");
List<String> myWordList = new ArrayList<String>();
// add all the words that contains your search word
for (String word : allWords)
if (word.contains(searchWord))
myWordList.add(word);
return myWordList;
}
答案 1 :(得分:-1)
File f = new File ("C:\\yourpath\\filename");
BufferedReader br = new BufferedReader(new FileReader(f));
String line = "";
while ((line = br.readLine()) != null) {
if (line.matches("good")) // regex here
System.out.println(line);
}
答案 2 :(得分:-1)
您也可以使用扫描仪:
Scanner scanner = null;
Pattern pattern = Pattern.compile("good[a-zA-Z]*\\p{Blank}*");
List<String> matches = new ArrayList<String>();;
try {
scanner = new Scanner(driver.getPageSource());//selenium driver
String match = "";
while (null != (match = scanner.findWithinHorizon(pattern, 0))){
matches.add(match.trim());
}
} catch (Exception e) {
}finally{
if(scanner != null){
scanner.close();
}
}