【问题标题】:driver.getcurrenturl () does not take the correct urldriver.getcurrenturl() 没有取到正确的url
【发布时间】:2017-04-28 20:51:50
【问题描述】:

我正在尝试学习 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");
    }

}

【问题讨论】:

  • 第一个调试问题:driver.getCurrentUrl(); 打印出什么?
  • 登录后会得到第二个URL详细信息,即“test.com/Home/Index”
  • 我没有问它会得到什么,而是要打印什么。换句话说,如果你把System.out.println(driver.getCurrentUrl()) 放在 if 检查里面,你会看到什么
  • 您确定将上述命令粘贴到您的代码中吗?
  • 当我在按钮单击事件后打印 driver.getcurrentUrl 时,显示的 url 保持不变,而在浏览器上,url 已更改为 test.com/home/index

标签: java selenium-webdriver automation


【解决方案1】:
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");
}

【讨论】:

  • public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("test.com/"); driver.findElement(By.name("SelectedDomainName")).sendKeys("x"); driver.findElement(By.id("UserName")).sendKeys("y" ); driver.findElement(By.id("Password")).sendKeys("123"); driver.findElement(By.id("loginBtn")).click(); String Url = driver.getCurrentUrl(); if (Url.equals("test.com/Home/Index")) { System.out.println("登录成功"); } else { System.out.println("登录失败"); } } }
  • 你要把那个 x 寄到哪里?在发送键下
  • x,y 和 123 只是域名、用户名和密码。
  • 我面临的问题是它不会采用当前的网址。它采用我在 driver.get("test.com/index.html"); 中指定的相同 URL
  • 但是你在哪里通过 test.com 上的这些东西。我去了那个网站,在那里我找不到域名、用户名、Loginbtn。我运行了你的代码,它给了我错误,因为没有找到这样的元素。所以请告诉你在 test.com 页面上将密钥发送到哪里?。
【解决方案2】:

您希望获得什么网址?确保首先导航到该 URL(见下文)并且导航已完成。

driver.get("http://www.google.com");

在将其与其他字符串进行比较之前,请将其打印出来并查看其中的内容。请注意,如果您在导航发生之前尝试获取 URL,它将返回 about:blank 字符串给您。

【讨论】:

  • @ShayniSood 我问的另一个问题是你的期望是什么,你得到了什么?
  • 我希望得到“test.com/Home/Index”,但我没有在 driver.getcurrent(URL) 中得到这个。相反,我得到的是:test.com"。这是我用来启动 fireforx 浏览器的 URL。
  • 你有什么帮助吗??
  • @ShayniSood 查看您的示例和您正在使用的网页让我认为您的脚本错误,因为我看不到您应该如何访问该网址.我什至无法手动执行此操作。请检查您的方案。
猜你喜欢
  • 2018-11-26
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 2020-01-29
  • 2021-10-28
  • 2018-05-28
相关资源
最近更新 更多