frozenade
frozenade

Reputation: 179

How to hide Selenium ChromeDriver console in VB .NET

How to convert these C# code to VB .NET? I just can't figured out.

var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var driver = new ChromeDriver(driverService, new ChromeOptions());

or

var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
var option = new ChromeOptions();
option.AddArguments("--headless", "--no-sandbox", "--disable-web-security", "--disable-gpu", "--incognito", "--proxy-bypass-list=*", "--proxy-server='direct://'", "--log-level=3", "--hide-scrollbars");
driver = new ChromeDriver(chromeDriverService, options);

I just want to hide window console while running.

Upvotes: 0

Views: 3598

Answers (1)

InteXX
InteXX

Reputation: 6367

Here are the conversions as produced by Telerik's Code Converter:

Dim driverService = ChromeDriverService.CreateDefaultService()
driverService.HideCommandPromptWindow = True
Dim driver = New ChromeDriver(driverService, New ChromeOptions())

...and

Dim chromeDriverService = ChromeDriverService.CreateDefaultService()
chromeDriverService.HideCommandPromptWindow = True
Dim [option] = New ChromeOptions()
[option].AddArguments("--headless", "--no-sandbox", "--disable-web-security", "--disable-gpu", "--incognito", "--proxy-bypass-list=*", "--proxy-server='direct://'", "--log-level=3", "--hide-scrollbars")
driver = New ChromeDriver(chromeDriverService, options)

--EDIT--

Here's the corrected code from your full sample:

Imports OpenQA.Selenium
Imports OpenQA.Selenium.Keys
Imports OpenQA.Selenium.Chrome
Imports System.Threading.Thread
Imports System.Data.Odbc
Imports System.Text.RegularExpressions

Public Class Form1
  Public Sub New()
    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    driverService = ChromeDriverService.CreateDefaultService()
    driverService.HideCommandPromptWindow = True
    driver = New ChromeDriver(driverService, New ChromeOptions())
  End Sub

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    driver.Navigate().GoToUrl("http://www.google.com")
  End Sub

  Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    btnStop.Text = "Stopping service..."
    btnStop.Enabled = False
    driver.Quit()
    driver.Dispose()
    btnStop.Text = "Done."
  End Sub

  Private driverService As ChromeDriverService
  Private driver As IWebDriver
End Class

Note that driver and driverService are Fields on the class. For more info on class fields, see the official documentation here:

Fields and properties

Fields and properties represent information stored in an object. You retrieve and set their values with assignment statements the same way you retrieve and set local variables in a procedure. The following example retrieves the Width property and sets the ForeColor property of a Label object.

Dim warningWidth As Integer = warningLabel.Width
warningLabel.ForeColor = System.Drawing.Color.Red

When you want to execute code against an Object's Property, such as setting driverService.HideCommandPromptWindow to True, that action must occur somewhere within a Method, Function or Property Accessor. In this case, that particular line of code is running within the form's constructor, which is a Method.

Those sections of the Microsoft documentation will be very good reads for you, when you get a chance.

Upvotes: 1

Related Questions