Selenium EventFiringWebDriver导航事件 [英] Selenium EventFiringWebDriver Navigated event

查看:291
本文介绍了Selenium EventFiringWebDriver导航事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Web驱动程序,我想听浏览器中发生的事件.例如,当页面上的某些JavaScript导致使用window.location = "...";

I am using Selenium web driver, I want to listen for events occurring in the browser. For instance, when some javascript on the page causes the url to change using window.location = "...";

我有一个侦听器附加到我的EventFiringWebDriverNavigated事件.但是,当更改浏览器的URL时,此操作将无法触发.我由此推断Selenium无法以这种方式收听事件?

I have a listener attached to the Navigated event of my EventFiringWebDriver. But this fails to fire when the browser's url is changed. I'm inferring from this that Selenium can't listen to the event in this way?

是否有适当的方法可以附加到浏览器或来自浏览器的DOM事件?

Is there a proper way to attach to browser or DOM events coming from the browser?

推荐答案

我能够使用OpenQA.Selenium.Support.UI.WebDriverWait对象来实现此目的.我只需要指定谓词(浏览器URL上的比较)和时间跨度即可. Selenium每500毫秒检查一次结果,直到结果为true或经过时间跨度(在这种情况下测试失败).不是完全由事件驱动的,但具有预期的效果:

I was able to use the OpenQA.Selenium.Support.UI.WebDriverWait object to achieve this. I just have to specify the predicate (a comparison on the browser url) and a timespan. Selenium checks the result once every 500ms until it returns true or the timespan elapses, in which case the test fails. Not quite event driven but it has the desired effect:

    private readonly IWebDriver _driver = new FirefoxDriver();

    [Test]
    public void UrlIsCorrect()
    {
        var w = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
        w.Until(driver => driver.Url == "http://test.com");
    }

这篇关于Selenium EventFiringWebDriver导航事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆