GCuser99 / SeleniumVBA

A comprehensive Selenium wrapper for browser automation developed for MS Office VBA running in Windows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can not set binary for portable browser (firefox)

surfer-silver opened this issue · comments

If need to use portable browser (firefox) there is no such option until Start raise error no browser found until i updated registry to make fool Start function suggest better way to use portable browsers.
create this REG_SZ and set the value to current version 108.0.2

HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox\CurrentVersion
after executing Start function created Capabilities and passed to OpenBrowser

after that SeleniumVBA downloaded the geckodriver at OpenBrowser passed the caps and this started

provide some way to use portable browsers only needed version number to download correct driver it can be configured

commented

Thanks for the report @surfer-silver.

Will think about how to best solve this. Couple things that come to mind in the short-term:

  1. You could turn off the driver auto-check-and-update feature via the SeleniumVBA.ini file if you are using SeleniumVBA v3.0 or later - that would skip the compatibility check against the browser, which is where I think it might be throwing the error. See Advanced Customization in the Wiki.

  2. On the development side, we could (maybe should!) handle that error so that it does not end the execution flow. In fact, that was one of the design goals of the auto-check-and-update feature, but I was not aware of your particular usage case at the time that code was written.

Of course, with both of those options, you would have to handle the driver-browser version alignment yourself. You can do that programmatically in the following way, which I do not believe involves the browser version:

Dim wdm As WebDriverManager
Dim dverInstalled As String
Dim dverLatest As String

Set wdm = New WebDriverManager

'wdm.DefaultDriverFolder = [your driver folder path here] 'defaults to Downloads dir

dverInstalled = wdm.GetInstalledDriverVersion(svbaBrowser.Firefox)
dverLatest = wdm.GetLatestDriverVersion(svbaBrowser.Firefox)

If dverLatest <> dverInstalled Then
    wdm.DownloadAndInstallDriver svbaBrowser.Firefox, dverLatest
End If

One assumption for the above logic to work is that you are keeping your browser version up to date. If the browser version falls far enough behind the latest driver version, then that could cause an incompatibility problem. So you might want to set your portable browser to auto-update mode, if you have that capability.

Would that work for you?

commented

@surfer-silver, I committed a new version that I believe will handle your portable browser situation. To test it, delete the register key/value that you used to fake the installed browser, and also delete the installed Geckodriver.exe. Then run the script that you described in your issue report. The auto-check-and-update feature of WebDriver class will now check if the browser is registered, and if not, will download the latest driver, if it is not already installed. It should not throw a "browser is not installed error." I hope that works for you.

By the way, if you are using the SetBrowserBinary method of the WebCapabilities class to set the path to your portable browser each time, then please know that you can use the SeleniumVBA.ini file to accomplish that automatically.

Here is how:

Run a procedure like this to create and save a json capabilities file:

Sub create_caps_json_file()
    Dim driver As WebDriver
    Dim caps As WebCapabilities

    Set driver = New WebDriver
    
    driver.StartFirefox
    
    Set caps = driver.CreateCapabilities
    
    'set some preferred capabilities
    caps.SetBrowserBinary "[your path to the executable]"
    
    'can set more preferences/arguments here...
    
    'save capabilities to a json file 
    caps.SaveToFile "[your path to preloaded caps file folder]\firefox.json"

    driver.Wait 1000
    driver.Shutdown
End Sub

Then modify the SeleniumVBA.ini file to something like this (see the section [FIREFOX] below):

# This settings file is completely optional. For it to have effect,
# it must be located in the same folder as the SeleniumVBA code library.
 
# If a value for an entry is not specified, then the system
# default value will be used.
 
# Note that all path-type entry values recognize the %[Environ]% syntax.
 
[GENERAL]
 
# The driver_location_folder system defaults to Downloads folder.
# The default_io_folder system defaults to the active vba project's
# document location - leave this blank to use default.
# Valid values for command_window_style are vbHide (default), 
# vbNormalFocus, vbMinimizedFocus, vbMaximizedFocus, vbNormalNoFocus,
# and vbMinimizedNoFocus.
 
driver_location_folder=%USERPROFILE%\Downloads
default_io_folder=
command_window_style=vbHide
 
[AUTO-DRIVER-UPDATE]
 
# If auto_detect_and_update=True (system default) then everytime
# the WebDriver's Start* method is called, the Selenium driver's
# version is checked against the corresponding browser version.
# If the driver is not compatible with browser, it will be updated.
# min_compatibility_level determines trigger for updating an
# an out-of-date driver. System default is svbaBuildMajor.
# Use svbaMinor for less frequent updating, and svbaExactMatch
# for more frequent updating.
 
auto_detect_and_update=True
min_compatibility_level=svbaBuildMajor
 
# Below are browser-specific initializations.
# To automatically initialize a set of capabilities each time the
# OpenBrowser method of WebDriver class is invoked, set the
# preload_capabilities_file_path entry to the path of a valid json
# capabilities file. Note that if preload_capabilities_file_path is
# set to a blank value, or the entry is missing or commented out,
# then this option is ignored. Use the SaveToFile method of the
# WebCapabilities class to save a default set of capabilities
# for pre-loading.
 
[CHROME]
 
preload_capabilities_file_path=
 
[EDGE]
 
preload_capabilities_file_path=
 
[FIREFOX]
 
preload_capabilities_file_path=[your path to preloaded caps file folder]\firefox.json
 
[INTERNET EXPLORER]
 
preload_capabilities_file_path=
 
[PDF_DEFAULT_PRINT_SETTINGS]
 
# Valid units values are svbaInches (default) or svbaCentimeters.
# Valid orientation values are svbaPortrait (default) or svbaLandscape.
 
units=svbaInches
page_height=11
page_width=8.5
margin_bottom=.393701
margin_top=.393701
margin_right=.393701
margin_left=.393701
background=False
orientation=svbaPortrait
print_scale=1.0
shrink_to_fit=True

Make sure the SeleniumVBA.ini file is located in the same folder as the code library. If you do all of that, then every time you run a SeleniumVBA script the path to the portable browser will be set automatically, without you having to create and initialize the capabilities object.

Please let us know if any of this was useful and thanks again for the issue report!

@GCuser99 many thanks for your prompt response appreciate your efforts related to SeleniumVBA project.

will update the status of such changes.

thanks bro