2017-02-13 133 views
0

我試圖通過比較'歡迎用戶'文本與用戶名來驗證登錄。 comapriosn不匹配。當我用elem.getText()打印xpath文本時,它會顯示包含選定文本的其他文本。無法通過xpath驗證登錄

任何人都可以提供有關如何編寫正確的xpaths或編寫更好的代碼來驗證登錄的建議嗎?

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.firefox.FirefoxProfile; 
import org.openqa.selenium.firefox.internal.ProfilesIni; 

public class verify_loign 
{ 
    public static void main(String []args) 
    { 
     WebDriver driver; 
     ProfilesIni profile = new ProfilesIni(); 
     FirefoxProfile fire_profile = profile.getProfile("sele_prac"); 
     driver=new FirefoxDriver(fire_profile); 
     driver.get("https://phptravels.org/clientarea.php"); 
      driver.findElement(By.id("inputEmail")).sendKeys("[email protected]"); 
     driver.findElement(By.id("inputPassword")).sendKeys("123456"); 
     driver.findElement(By.id("login")).submit(); 
     WebElement verify_login_ele =  driver.findElement(By.xpath("//div[@class='header-lined' and contains(h1,'Welcome Back, rakesh')]")); 
//  String str = "Welcome Back, rakesh"; 
//  if(verify_login_ele.equals(str)) 
//  { 
//   System.out.println("Your logged in succesfully"); 
//  } 
//  else 
//  { 
//   System.out.println("Something went wrong with valid credential"); 
//  } 
     System.out.println(verify_login_ele.getText()); 
     driver.findElement(By.id("Secondary_Navbar-Account")).click(); 
     driver.findElement(By.id("Secondary_Navbar-Account-Logout")).click(); 
     driver.close(); 
     driver.quit(); 
    } 
} 
+0

不是每個人都有帳戶phptravels.org ...你能分享'提到歡迎HTML'消息? – Andersson

回答

0

我剛剛用FirePath檢查過它。

WebElement welcomeElement = driver.findElement(By.xpath("//h1")); 

這個網站上只有一個h1標題。

+0

謝謝Grzegorz –

2

它當前失敗的原因是因爲您試圖將WebElement對象與文本進行比較,而這些文本始終會失敗。有幾種可供選擇的方式來完成你正在努力完成的任務。

的最快最簡單的「修復」將插入.equals前.getText()(...

if(verify_login_ele.getText().equals(str)) 

它不是最完美的解決方案,但它很可能是所有需要使它工作。

不要擔心,這是一個初學者的錯誤,你做的很好,到目前爲止。

+0

謝謝比爾現在工作正常 –