【问题标题】:How can you start a browser at a specific URL using the selenium webdriver如何使用 selenium webdriver 在特定 URL 启动浏览器
【发布时间】:2018-05-04 21:59:44
【问题描述】:

如果我执行以下操作:

 driver = webdriver.Chrome() # this results in the browser displaying the about page
 driver.get("http://somesite.com/")  # now the browser goes to the URL

然后,如果我通过浏览器中的 javascript 控制台检查历史长度,我得到的值为 2。 我需要模拟使用 URL 打开新选项卡或窗口的情况,因此历史长度为 1。

提前致谢。

【问题讨论】:

    标签: selenium webdriver


    【解决方案1】:

    您可以在初始化 WebDriver 时设置一些特定的标志以传递给它。您可以查看示例 here(适用于 Chrome),还有一个指向完整开关列表的链接。以下是如何在 java 中设置 ChromeDriver 的主页:

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability("chrome.switches",
            Arrays.asList("--homepage=http://somesite.com/"));
    WebDriver driver = new ChromeDriver(capabilities);
    

    【讨论】:

    • 我还没有尝试过这个.. 但是如果是这种情况,那么我更喜欢这种方法
    【解决方案2】:

    options.AddArgument("--homepage \"url\""); 像这样,设置空主页: options.AddArgument("--homepage \"data:,\"");

    【讨论】:

      【解决方案3】:

      我认为您在这里有 3 种可能的选择。 (1 可能不是)

      选项 1:

      设计你自己的get 方法来执行javascript。就像是, window.location.replace("http://somesite.com/") 所以它不会被记录到历史记录中。

      选项 2:

      在您的验证中,只需确保您验证 n-1 以“假装”它不存在。

      选项3:(不确定这种可能性)

      使用JavascriptExecutor,找出一种可以修改history 对象的方法,然后取出最初的about:blank 历史条目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-16
        • 1970-01-01
        • 1970-01-01
        • 2017-01-01
        相关资源
        最近更新 更多