【问题标题】:Selenium junit test failing, driver taking URL too soonSelenium junit 测试失败,驱动程序太快获取 URL
【发布时间】:2021-05-04 09:24:06
【问题描述】:

我正在努力让这个测试通过。 我正在检查网页 URL 是否与预期的 URL 相同。

但是,驱动程序会在下一页加载之前不断获取 URL,因此它会不断检查旧 URL 与预期的 URL。

    driver.findElement(By.id("keywords")).sendKeys("Arlo Lamp");
    driver.findElement(By.className("search-form")).submit();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='onetrust-accept-btn-handler']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ol[@class='product-list product-list-main']/li//a"))).click();
    
    String url = driver.getCurrentUrl();
    System.out.println(url);
    assertEquals("https://www.dunnesstores.com/p/arlo-lamp/7761102?colour=Cream", url);

这应该是它断言的页面

相反,它是点击它一直断言的项目之前的页面

【问题讨论】:

    标签: java selenium driver


    【解决方案1】:

    您可以像这样等待该 URL:

    new WebDriverWait(driver, 10)
            .until(webDriver -> webDriver.getCurrentUrl().equals("https://your.url"));
    

    或等待某个特定于该页面的 UI 元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 2016-01-28
      • 2016-09-08
      • 1970-01-01
      • 2017-07-04
      相关资源
      最近更新 更多