由于我是 Selenium 和 Java 的新手,所以在測驗登錄頁面時遇到了困難。下面是我試圖測驗的代碼,Test case failed即使我提供了正確的用戶名和密碼,它也始終回傳。我想知道哪里出了問題。檢查相等條件時,URL 也是正確的。我在登錄后獲取的 URL。
package Seleniumtesting;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\hp\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver d = new ChromeDriver();
d.get("https://onelogin.adityabirlacapital.com/login");
d.findElement(By.id("login-id")).sendKeys("my_username");
d.findElement(By.id("password")).sendKeys("my_password");
//d.findElement(By.xpath("//*[@id=\"password-login\"]")).click();
d.findElement(By.id("password-login")).click();
String u = d.getCurrentUrl();
if(u.equalsIgnoreCase("https://onelogin.adityabirlacapital.com/my-dashboard"))
{
System.out.println("Test case passed");
}
else
{
System.out.println("Test case failed");
}
d.close();
}
}
uj5u.com熱心網友回復:
您可以嘗試在執行之前添加延遲d.getCurrentUrl()。類似于TimeUnit.SECONDS.sleep(2);您的代碼以確保在單擊登錄后檢索當前 URL 的值時加載了所有內容。
其他解決方案可能是,在通過 Selenium 單擊登錄按鈕后,在主頁中查找可能表明用戶已成功登錄的任何元素,例如歡迎訊息或是否已有注銷按鈕。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/404732.html
標籤:
