Selenium with chrome shows console window of chrome driver , get page complete events

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

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") 

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?

EDIT

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.

EDIT

What I did is

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 

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?

回答1:

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) 

It's generally considered bad form to ask multiple unrelated questions in a single post on StackOverflow, 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.

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.

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 



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!