Selenium with chrome shows the console window of the chrome driver, get completed pages

I use Selenium to automate my Chrome browser. I want to open a web page and fill out a login form, submit, etc. Below is a piece 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 is working correctly. But when I launch the application, a console window appears when the chrome opens. This is related to chromedriver.exe. And cond = sole windows shows "Starting ChromeDriver (v2.9.248315) on port 2078". But I do not want this console window to appear, because the application for the application for users does not require it and does not understand what it is. So is there a way to avoid this console window?

Also, how can I get the final pages as soon as the page is fully loaded?

EDIT

As in Document Completed , which is available for web browser controls. After reading the selenium documentation, I found that there is an EventFiringWebDriver that can do what I want (e.g. OnNavigated). And chromeDriver doesn't have these. But I have not received any example of how this (EventFiringWebDriver) can be used with Onnavigated functions, etc., to receive 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 navigating the OnNavigated page, the function starts, as I expected. But after the page is loaded, and the username and password are filled in, and if the user presses the login button manually, a new page will appear after logging in. During this new page, the navigation system does not start OnNavigated. But I want to receive notifications for every page that moves even from user inputs. So how can this be achieved?

0
source share
1 answer

The default command line window display behavior is a function, not an error. Many people insist on using the .Close() method to exit the browser window instead of .Quit() . However, this will close the browser, but will not clear all resources, for example, exit the chromedriver.exe instance. Thus, the supporting .NET linker made a conscious decision to make it absolutely clear when the executable works and when not. You can change this behavior 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 is generally believed that a bad form asks several unrelated questions in a single post on StackOverflow, but as for your second question, you ask: "How do I know when a page is fully loaded using WebDriver?" If so, then the answer will be: "You will not do it." Or rather, the question is pointless when you talk about today's world with JavaScript, AJAX, dynamically-generated DOM support.

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

You can also try using EventFiringWebDriver , which has a Navigated event. To do this, use the standard VB.NET event processing code ( AddHandler / RemoveHandler ). However, DO NOT associate this .NET binding event with the type of events you receive from the Internet Explorer COM object, such as the DocumentComplete event . This design does not exist in the WebDriver API. The Navigated event occurs only when manually navigating the page (i.e., not by the clicks of an element that causes navigation), and there is no guarantee that any specific DOM event will be fired when the Navigated event Navigated . The code to use will 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 can’t stress this enough if you are looking for an event that means "page loaded", "Do Do It It Wrong β„’". The right thing is to identify some element of the landing page that will be present and wait until this element is available.

+3
source

Source: https://habr.com/ru/post/1269517/


All Articles