我正在尝试学习 selenium webdriver,因此我从基础知识开始,但我的 driver.getcurrenturl()
没有采用正确的 url。
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://test.com/");
driver.findElement(By.name("SelectedDomainName")).sendKeys("a");
driver.findElement(By.id("UserName")).sendKeys("b");
driver.findElement(By.id("Password")).sendKeys("#123");
driver.findElement(By.id("loginBtn")).click();
String Url = driver.getCurrentUrl();
if (Url.equals("http://test.com/Home/Index")) {
System.out.println("Login successful");
} else {
System.out.println("Login Failed");
}
}
最佳答案
String url = driver.getCurrentUrl();
这将获取当前网址,但您需要先访问某个网址,因此您需要提及:
driver.get("https://www.test.com/index.html");
之后您可以将其放入字符串并验证。
WebDriver driver = new FirefoxDriver();
driver.get("https://www.test.com/index.html");
String url = driver.getCurrentUrl();
if(url.equals("https://www.test.com/index.html")) {
System.out.println("Login successful");
} else {
System.out.println("Incorrect details provided by the User");
}
关于java - driver.getcurrenturl() 没有获取正确的url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37175666/