【问题标题】:Method getCurrentUrl(); returns incorrect URL方法 getCurrentUrl();返回不正确的 URL
【发布时间】:2018-10-31 17:53:24
【问题描述】:

我实际上刚刚开始使用 Selenium 学习 Java,但我无法接收当前的 URL。 Selenium 返回起始 URL,而不是当前 URL。

看起来,implicitlywait() 不起作用,还是我做错了什么?

package SeleniumPackage;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;

public class SeleniumClass {

public static void main(String[] args) {

WebDriver driver = new ChromeDriver();

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.get("https://learn.letskodeit.com/p/practice");

WebElement benzRadioBtn = driver.findElement(By.id("benzradio"));
benzRadioBtn.click();

WebElement hondaRadioBtn = driver.findElement(By.id("hondaradio"));
hondaRadioBtn.click();
WebElement bmwRadioBtn = driver.findElement(By.id("bmwradio"));
bmwRadioBtn.click();
WebElement benzCheckBox = driver.findElement(By.id("benzcheck"));
benzCheckBox.click();
Select dropdown = new Select (driver.findElement(By.id("carselect")));
dropdown.selectByValue("honda");
WebElement element = driver.findElement(By.id("product"));
System.out.println(element.getText());

driver.findElement(By.linkText("Terms of Use")).click();

String currentURL = driver.getCurrentUrl();  
System.out.println(currentURL);

【问题讨论】:

标签: java selenium selenium-webdriver


【解决方案1】:

您必须在单击“使用条款”链接后添加一些明确的等待,否则会添加一些延迟。

修改后的代码:

driver.findElement(By.linkText("Terms of Use")).click();
//Added Newly
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.titleContains("Terms"));

String currentURL = driver.getCurrentUrl();

【讨论】:

  • 这也是正确的,但我们也可以使用验证点:P
  • 是的。但有时,页面标题功能也不会正确给出当前页面标题。所以,最好在页面加载动作之后添加一些等待,然后我们可以添加验证
【解决方案2】:

这是一种预期行为,因为您单击 iframe 的“使用条款”,这不会重定向您的 URL(只需更改 iframe)

我建议使用 xpath 来查找所需的元素,如下所示:

WebElement element = driver.findElement(By.xpath("/html[@class=' video no-videoautoplay']/body/div[@class='view-school']/footer[@class='bottom-menu bottom-menu-inverse']/div[@class='container']/div[@class='row']/div[@class='col-xs-12 col-sm-4 col-md-4 footer-column'][2]/ul[@class='list-unstyled']/li[1]/a"));

【讨论】:

  • 你是如何得到这个 Xpath 的?谢谢。
  • 通过 Chrome 扩展,我暂时不记得名字了。
【解决方案3】:

如果标题匹配,你可以匹配标题然后打印当前 URL,它总是给你正确的 URL

public static void main(String[] args) throws InterruptedException {
System.out.println("Ready to launch the browser");
System.setProperty("webdriver.chrome.driver", "C:/Users/sankalp.gupta/Desktop/JAVASELN/chromedriver.exe");

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://learn.letskodeit.com/p/practice");

WebElement benzRadioBtn = driver.findElement(By.id("benzradio"));
benzRadioBtn.click();

WebElement hondaRadioBtn = driver.findElement(By.id("hondaradio"));
hondaRadioBtn.click();
WebElement bmwRadioBtn = driver.findElement(By.id("bmwradio"));
bmwRadioBtn.click();
WebElement benzCheckBox = driver.findElement(By.id("benzcheck"));
benzCheckBox.click();
Select dropdown = new Select (driver.findElement(By.id("carselect")));
dropdown.selectByValue("honda");
WebElement element = driver.findElement(By.id("product"));
System.out.println(element.getText());

driver.findElement(By.linkText("Terms of Use")).click();
if(driver.getTitle().contains("Practice | Let's Kode It"))
    {
        System.out.println(driver.getCurrentUrl());

    }
}

【讨论】:

  • 这个方法没有给我想要的结果。谢谢。
  • 你想要什么结果
  • 更改 iframe 后获取当前 url。
【解决方案4】:

正如您在上一行中调用了click(),接下来要检索URL,您需要结合ExpectedConditions 诱导WebDriverWait方法urlContains(java.lang.String fraction)如下:

new WebDriverWait(driver, 20).until(ExpectedConditions.urlContains("partial_text_of_url"));
System.out.println(driver.getCurrentUrl());

注意:您需要删除implicitlywait(),因为根据documentation

不要混合隐式和显式等待。这样做可能会导致无法预测的等待时间。例如,设置 10 秒的隐式等待和 15 秒的显式等待,可能会导致 20 秒后发生超时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多