2016-02-12 28 views
0

我試圖通過它下面的代碼: -如何比較網頁上的文字與網頁驅動程序中外部文件中的文字?

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"); 
    } 

但上面的代碼是不工作對我來說...需要幫助urgntly

+0

更新我的回答 –

回答

0

這些都是necersary進口:

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

而且這是一種方法,它允許你通過將文件名作爲參數傳遞給File來讀取它: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"); 
} 

希望它會幫助你:)

+0

請接受答案,如果它真的幫助你通過點擊我的答案左邊的正確的標誌,並隨時投票..它會真的幫助我:) ..下面,請參閱如果y你不知道如何接受答案http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –

+0

你爲什麼建議OP使用'equalsIgnoreCase'而不是原來的' equals'? –