【问题标题】:Can't set PageLoad of ChromeDriver instance无法设置 ChromeDriver 实例的 PageLoad
【发布时间】:2017-12-06 19:27:40
【问题描述】:

在这里潜伏了很长时间,现在是我第一次真正的帖子的时候了。 我目前正在.NET 中使用 Selenium 为我们的应用程序开发跨浏览器测试。

按照http://www.guru99.com/cross-browser-testing-using-selenium.html,我实现了这个:

[SetUpFixture]
public class TestSetup()
{
    public static IWebDriver driver;
    ...

    [OneTimeSetUp]
    public void GlobalSetup()
    {
        if(browserToTest.Equals("Firefox"))
        {
              driver = new FirefoxDriver();
        }
        else if(browserToTest.Equals("Chrome"))
        {
              driver = new ChromeDriver();
        }

        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10.00);
        driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10.00);

        // navigate to url, login, etc. pp
    }
}

在“Firefox”的情况下,一切正常。 “Chrome”案例 - 与教程相反 - 给了我这个错误:

OneTimeSetUp: System.NotImplementedException : 驱动程序实例必须符合 W3C 规范才能支持获取超时值。

我正在使用:

  • Firefox v54.0
  • Chrome v59.0.3071.115
  • Selenium.WebDriver v3.4.0
  • Selenium.Firefox.WebDriver v0.17.0
  • Selenium.WebDriver.ChromeDriver v2.30.0.1

我可以为 chrome 设置 ImplicitWait 和 PageLoad,还是只为 firefox 实现?

【问题讨论】:

  • 我很好奇为什么您在构建代码时没有收到错误消息。 driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10.00); 是超时,不是超时。
  • 我没有复制 src 1:1,那只是一个愚蠢的错字:) 更正它
  • 您的代码在我的机器上运行良好。我认为这是一个兼容性问题。你能从/bin卸载清理chromedriver然后重新安装吗?
  • 抱歉,我花了这么长时间,我还没有时间进一步开发我们的测试。无论如何,这实际上是某种兼容性问题。在我的新工作站上检查项目后,它立即工作。
  • 你应该检查这个 repo:github.com/bonigarcia/webdrivermanager 以避免这些问题。它是一个自动化的网络驱动程序管理器,可为您获取正确的驱动程序和版本

标签: .net google-chrome selenium firefox cross-browser


【解决方案1】:

你可以试试

driver.manage().timeouts().implicitlyWait(10,timeunit.seconds)
driver.manage().timeouts().pageLoadTimeout(10,timeunit.seconds)

这是使用 selenium 3

【讨论】:

    【解决方案2】:

    我使用了这段代码,它在 chrome 和 firefox 中都适用。 PageObject 是我的类,waitPageToLoad 是我的方法。

    public class PageObject {
        protected Logger log = LogManager.getLogger(this.getClass().getName());
        protected WebDriver driver;
        protected WebDriverWait wait;
    
      public WebDriver getDriver() {
        log.debug("obtaining the driver object for current thread");
        return driver;
    } 
     public WebDriverWait getWait() {
        log.debug("obtaining the wait object for current thread");
        return wait;
    }
     public void initialise(Object obj) {
        PageFactory.initElements(getDriver(), obj);
    }
    
     public PageObject waitPageToLoad() {
            domLoaded();
            jqueryLoaded();
            return this;
        }
    
     public void domLoaded() {
            log.debug("checking that the DOM is loaded");
            final JavascriptExecutor js = (JavascriptExecutor) getDriver();
            Boolean domReady = js.executeScript("return document.readyState").equals("complete");
    
            if (!domReady) {
                getWait().until(new ExpectedCondition<Boolean>() {
                    public Boolean apply(WebDriver d) {
                        return (js.executeScript("return document.readyState").equals("complete"));
                    }
                });
            }
        }
    
      private void jqueryLoaded() {
            log.debug("checking that any JQuery operations complete");
            final JavascriptExecutor js = (JavascriptExecutor) getDriver();
    
            if ((Boolean) js.executeScript("return typeof jQuery != 'undefined'")) {
                boolean jqueryReady = (Boolean) js.executeScript("return jQuery.active==0");
    
                if (!jqueryReady) {
                    getWait().until(new ExpectedCondition<Boolean>() {
                        public Boolean apply(WebDriver d) {
                            return (Boolean) js.executeScript("return window.jQuery.active === 0");
                        }
                    });
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      相关资源
      最近更新 更多