带有Selenium的chrome显示chrome驱动程序的控制台窗口,获取页面完成事件

带有Selenium的chrome显示chrome驱动程序的控制台窗口,获取页面完成事件

问题描述:

我正在使用Selenium进行chrome浏览器自动化.我想打开一个网页并填写登录表格,提交等. 下面是代码的一部分

I am using Selenium for chrome browser automation. I want to open a webpage and fill a login form , submit etc. Below is a part of code

    Dim chromeOptions As New OpenQA.Selenium.Chrome.ChromeOptions()
    chromeOptions.AddExcludedArgument("ignore-certifcate-errors")
    chromeOptions.AddArgument("test-type")
    Dim driver As IWebDriver = New ChromeDriver(chromeOptions)
    driver.Navigate().GoToUrl("https://example.com")


    Dim myLink1 As IWebElement = driver.FindElement(By.Name("userName"))
    myLink1.SendKeys("username")
    Dim myLink2 As IWebElement = driver.FindElement(By.Name("password"))
    myLink2.SendKeys("mypassword")

它正常工作.但是,当我运行该应用程序时,Chrome打开后会出现一个控制台窗口.这是由于chromedriver.exe所致,cond =单独的窗口显示在端口2078上启动ChromeDriver(v2.9.248315)".但是我不希望此控制台窗口出现,因为用户起诉应用程序不需要它并了解它是什么.那么有什么办法可以避免此控制台窗口?

It works properly. But when i run the application a console window appears when chrome gets open. It is due to chromedriver.exe And cond=sole windows shows "Starting ChromeDriver (v2.9.248315) on port 2078". But I don't want this console window to appear as user suing application will not require it and understand what it is.So is there any way to avoid this console window?

页面完全加载后,如何获取页面完成事件?

Also how can I get page complete events once the page gets loaded completely?

编辑

类似于文档已完成可用于Web浏览器控件.阅读硒文档后,我发现存在一个 EventFiringWebDriver 可能会做我想要的事情(例如OnNavigated),而chromeDriver没有这些.但是我没有得到任何示例(EventFiringWebDriver)如何与Onnavigated函数等一起使用来获取这些事件.

Like in Document complete which is available for web browser controls. After reading selenium documentation I found that there exists a EventFiringWebDriver which may do what I want(like OnNavigated ) And chromeDriver does not have these. But i didnt get any example of how it(EventFiringWebDriver ) can be used with Onnavigated functions etc to get these events.

编辑

我所做的是

Imports OpenQA.Selenium
Imports OpenQA.Selenium.Chrome
Imports OpenQA.Selenium.Support.Events

Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService
Dim driver1 As ChromeDriver = Nothing
Dim driver As EventFiringWebDriver
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim chromeOptions As New OpenQA.Selenium.Chrome.ChromeOptions()
        chromeOptions.AddExcludedArgument("ignore-certifcate-errors")
        chromeOptions.AddArgument("test-type")
        service.HideCommandPromptWindow = True
        driver1 = New ChromeDriver(service, chromeOptions)
        driver = New EventFiringWebDriver(driver1)
        AddHandler driver.Navigated, AddressOf OnNavigated
        driver.Navigate().GoToUrl("https://example.com")
        Dim myLink1 As IWebElement = driver.FindElement(By.Name("userName"))
        myLink1.SendKeys("username")
        Dim myLink2 As IWebElement = driver.FindElement(By.Name("password"))
        myLink2.SendKeys("mypassword")
    End Sub
    Protected Sub OnNavigated(ByVal sender As Object, ByVal e As Support.Events.WebDriverNavigationEventArgs)
        MessageBox.Show("Page navaigated  " & e.Url)
    End Sub
    
End Class

页面导航后,按预期触发了OnNavigated函数.但是在浏览页面并填写用户名和密码之后,如果用户手动单击登录按钮,则登录后将出现一个新页面.在这个新页面被导航期间,它不会触发OnNavigated.但是,我想获得即使是用户输入也能导航的每个页面的通知.那么如何实现呢?

After page gets navigated OnNavigated function triggered as I expected. But after page gets navigated and user name and password filled and if user clicks on login button manually a new page will appear after login. During this new page gets navigated it wont trigger OnNavigated .But i want to get notification for every page that gets navigated even from user inputs. So how this can be achieved?

显示命令提示符窗口的默认行为是功能,而不是错误.许多人坚持使用.Close()方法而不是.Quit()退出浏览器窗口.但是,这将关闭浏览器,但不会清除所有资源,例如退出chromedriver.exe实例.因此,.NET绑定维护者做出了一个有意识的决定,使它在可执行文件何时运行以及何时未运行时绝对清楚.您可以通过使用类似于以下代码的方式来更改此行为:

The default behavior of displaying the command prompt window is a feature, not a bug. Many people insist on using the .Close() method to exit the browser window instead of .Quit(). However, this would close the browser, but not clean up all resources, like exiting the chromedriver.exe instance. Thus, the .NET bindings maintainer made a conscious decision to make it absolutely clear when the executable is running and when it isn't. You can change this behavior by using code similar to the following:

Dim service As OpenQA.Selenium.Chrome.ChromeDriverService = OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService()
service.HideCommandPromptWindow = True

Dim chromeOptions As New OpenQA.Selenium.Chrome.ChromeOptions()
chromeOptions.AddExcludedArgument("ignore-certifcate-errors")
chromeOptions.AddArgument("test-type")
Dim driver As IWebDriver = New ChromeDriver(service, chromeOptions)

在*上的单个帖子中询问多个不相关的问题通常被认为是错误的形式,但是对于第二个问题,您是否在问:我怎么知道何时使用WebDriver完全加载了页面?"如果是这样,那么答案是您不这样做".或者,更准确地说,当您谈论当今的JavaScript繁重,启用AJAX的动态生成DOM世界时,这个问题毫无意义.

It's generally considered bad form to ask multiple unrelated questions in a single post on *, but as for your second question, Are you asking, "How do I know when a page is completely loaded using WebDriver?" If so, then the answer is, "You don't." Or, more accurately, the question is meaningless when you're talking about today's JavaScript-heavy, AJAX-enabled, dynamically-generated-DOM world.

如果需要确保页面上存在某个元素然后才能与之交互,则可以等待该条件.支持库(在.NET中的WebDriver.Support.dll程序集中)包含一个WebDriverWait类,旨在完全启用该类.

If you need to make sure an element is present on a page before you can interact with it, you can wait for that condition. The support library (in the WebDriver.Support.dll assembly in .NET) contains a WebDriverWait class designed to enable exactly that.

您还可以尝试使用具有Navigated事件的EventFiringWebDriver.您将使用标准的VB.NET事件处理代码对此进行关联(AddHandler/RemoveHandler).但是, 请勿 将此.NET绑定事件与您从Internet Explorer的COM对象(如其 DocumentComplete事件. WebDriver API中不存在此类构造.仅当您手动导航到页面时(即不在引起导航的元素单击上),才会引发Navigated事件,并且不能保证在引发Navigated事件时会触发任何特定的DOM事件.使用它的代码如下所示:

You could also try using the EventFiringWebDriver, which has a Navigated event. You would use standard VB.NET event handling code to hook this up (AddHandler/RemoveHandler). However, DO NOT conflate this .NET bindings event with the type of events you receive from Internet Explorer's COM object, like its DocumentComplete event. No such construct exists in the WebDriver API. The Navigated event is only raised when you manually navigate to a page (i.e., not on element clicks that cause navigation), and there is no guarantee whatsoever that any particular DOM event will have fired when the Navigated event is raised. The code to use it would look something like this:

' Assumes you have a sub with a declaration like this:
' Private Sub Navigated(sender As Object, e As WebDriverNavigationEventArgs)
Dim driver As IWebDriver = New ChromeDriver(service, chromeOptions)
Dim eventDriver As New EventFiringWebDriver(driver)
AddHandler eventDriver.Navigated, AddressOf Navigated

再一次,如果您正在寻找一个意味着页面已加载"的事件,那么我就不能再强调太多了,这是您做错了.正确的做法是识别目标页面上将要显示的某些元素,然后等待该元素可用.

Once again, and I cannot stress this enough, if you're looking for an event that means "the page is loaded," You're Doing It Wrong™. The right thing to do is identify some element on the target page that will be present, and wait for that element to be available.