0

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 Answer 1

3

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

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.

6
  • Ok. After reading selenium documentation i found that there exists a EventFiringWebDriver(selenium.googlecode.com/svn@7084/trunk/docs/api/dotnet/…) 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. Can u please help? Aug 4, 2014 at 11:56
  • Edited my answer. It probably isn't the answer you want to hear, but you're approaching the problem in a way that differs drastically from the WebDriver API's approach. You're applying an incorrect and outdated paradigm to what you're trying to achieve.
    – JimEvans
    Aug 4, 2014 at 15:20
  • Thank you for the answer. I have another small doubt.Please check my edited question. Aug 5, 2014 at 6:50
  • It sounds like you're still trying to get a single "page is completely loaded" state from WebDriver. Let me try to be as clear and blunt as I possibly can. What you are looking for does not exist in WebDriver! If you could be more specific as to why you think you need this construct, someone might be better able to guide you in a more suitable direction.
    – JimEvans
    Aug 5, 2014 at 10:54
  • Ok. but by using my code i was able to navigate to page and after page gets loaded OnNavigated triggered properly. It worked. But after the login again another new page gets loaded , for this page OnNavigated didn't trigger. Another question is u said we cant trace whether page is really loaded. but what does driver.Navigated handler do then? Aug 6, 2014 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.