如何将网页上的文字与网页驱动程序中外部文件中的文本进行比较?

时间:2016-02-12 12:34:25

标签: selenium selenium-webdriver webdriver

我正在通过以下代码尝试: -

BufferedReader in = new BufferedReader(new FileReader("TC5_data.txt"));
String str;
while ((str = in .readLine()) != null) {
 System.out.println(str); {
  String str1 = driver.findElement(By.xpath("//*[@id='content']/div[2]/table/tbody/tr/td[1]")).getText();
  if (str.equals(str1)) {
   System.out.println("Data is Matching");
  } else {
   System.out.println("Data is not Matching");
  }

但上面的代码对我不起作用......急需帮助

1 个答案:

答案 0 :(得分:0)

这些是necersary进口:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

这是一个允许您通过将文件名作为参数传递给文件来读取文件的方法:readFile(“yourFile.txt”);

String readFile(String fileName) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append("\n");
            line = br.readLine();
        }
        return sb.toString();
    } finally {
        br.close();
    }
}

要比较您需要使用equalsIgnoreCase功能

的字符串

使用下面的条件代码: -

if(str.equalsIgnoreCase(str1))
{
    System.out.println("Equal");
}else{
    System.out.println("Not Equal");
}

希望它会对你有所帮助:)。