【问题标题】:Detecting URL change using Selenium WebDriver (JavaScript) [duplicate]使用 Selenium WebDriver (JavaScript) 检测 URL 更改 [重复]
【发布时间】:2019-05-13 12:08:47
【问题描述】:

我正在使用 selenium Web Driver 进行测试。问题是当我的 url 更改时 getCurrentURL 给了我当前的 URL。我为此使用 driver.sleep 方法。但是 driver.sleep 方法的问题是我必须通过固定时间让它在我的 URL 更改后运行。是否有任何方法可以在我的 URL 更改时运行(寻找一些不需要明确声明时间的睡眠替代方法)?

【问题讨论】:

  • 您查看文档了吗?就在里面。
  • 这是关于在 Javascript 中实现 Selenium Webdriver

标签: javascript selenium selenium-webdriver


【解决方案1】:

为此有 waituntil 模块

driver.wait(until.urlIs('expected url'));

【讨论】:

  • 实际上我想检查浏览器是否将我带到另一个网址。如果它带我到下一条路线,我将显示“测试通过”,如果没有,我将显示“测试失败”。我已经实施了你的解决方案,上面写着(node:800) UnhandledPromiseRejectionWarning: NoSuchSessionError: Tried to run command without establishing a connection
【解决方案2】:

您找到一种方法来等待特定行为而不是使用固定时间的方法绝对是正确的。

您执行此操作的方式取决于您如何实施它。例如,如果您使用 Protractor 框架,则以下内容应该可以工作:

var EC = browser.ExpectedConditions;
browser.wait(EC.urlContains('Expected URL'), timeout); // Checks that the current URL contains the expected text
browser.wait(EC.urlIs('Expected URL'), timeout); // Checks that the current URL matches the expected text

无论如何,希望你能根据你的情况调整这个逻辑。

【讨论】:

    【解决方案3】:

    我们可以通过使用轮询技术来实现这一点。编写一个循环以每 2 秒检查一次当前 URL 是否更改。这是示例代码。

    String currentUrl = driver.getCurrentUrl();
    String newUrl ;
    do {
         newUrl = driver.getCurrentUrl();
         Thread.sleep(5);
    }while(newUrl.contentEquals(currentUrl));
    //if control came out of loop , then you have new url
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 2011-08-15
      • 2018-07-04
      • 2014-08-21
      相关资源
      最近更新 更多