由于我刚接触Selenium和Java,所以在测试登录页面时被卡住了。下面是我的代码,我正在尝试测试,它总是返回Test case failed,即使我给出了正确的用户名和密码。我想知道哪里出了问题。在检查相同条件时,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();
}
}发布于 2022-01-05 07:09:48
您可以尝试在执行d.getCurrentUrl()之前添加延迟。在您的代码中添加一些类似TimeUnit.SECONDS.sleep(2);的内容,以确保在单击登录后,所有内容都已加载,检索当前URL的值。
其他解决方案可能是,在通过Selenium单击登录按钮之后,在主页中找到任何可能指示用户成功登录的元素,例如欢迎消息,或者如果已经有注销按钮。
https://stackoverflow.com/questions/70582206
复制相似问题