GCuser99 / SeleniumVBA

A comprehensive Selenium wrapper for browser automation developed for MS Office VBA running in Windows
MIT License
89 stars 18 forks source link

Browser Window only appears as 800 x 600 #102

Closed Vesuvex closed 7 months ago

Vesuvex commented 7 months ago

First of all this is a really good project and I am totally impressed by this.

Is there any way to change the default browser size to more than 800 x 600 without setting the size via driver.window or driver.activewindow?

I cant seem to change the default size and the viewport is stuck at 800,600

Thank you so much for developing this!

GCuser99 commented 7 months ago

Hi @Vesuvex - glad you are finding this project useful. It seems the default browser window size is system/screen resolution dependent. The default browser window size on my laptop is 1050x732.

One thing you could try is to set the window size through capabilities:

Sub test_size()
    Dim driver As SeleniumVBA.WebDriver
    Dim caps As SeleniumVBA.WebCapabilities

    Set driver = SeleniumVBA.New_WebDriver

    driver.StartChrome

    Set caps = driver.CreateCapabilities()

    'if this works for you, then preload capabilities so
    'that it defaults each time you call OpenBrowser method
    caps.AddArguments "--window-size=1000,750"

    driver.OpenBrowser caps 'here is where caps is passed to driver

    driver.NavigateTo "https://www.wikipedia.org/"

    Debug.Print "width: " & driver.ActiveWindow.Bounds("width")
    Debug.Print "height: " & driver.ActiveWindow.Bounds("height")

    driver.CloseBrowser
    driver.Shutdown
End Sub

If the above works for you then you could pre-load the capabilities via the SeleniumVBA ini file as explained in the Wiki under Advanced Customization.

Let us know if going in that direction solves your problem.

Mike

Vesuvex commented 7 months ago

Hi Mike,

Thanks for the quick response

Unfortunately, the above is not working for me.

Below is what my Edge Looks like and also my code.

image

` Dim driver As SeleniumVBA.WebDriver Dim keys As SeleniumVBA.WebKeyboard Dim actions As SeleniumVBA.WebActionChain Dim elem As SeleniumVBA.WebElement Dim frame1 As SeleniumVBA.WebElement Dim frame2 As SeleniumVBA.WebElement Dim caps As SeleniumVBA.WebCapabilities Dim edgeWindow As Long

Dim kron_wbk As Workbook
Dim p_vars As Worksheet
Dim kron_look As Worksheet
Dim ftime_kron As Worksheet
Dim empnum, upload_lrow, ftime_lrow As Integer
Dim ftloop As Range
Dim tbl1 As ListObject
Dim zz As Integer

Set kron_wbk = ActiveWorkbook
Set kron_look = kron_wbk.Sheets("KLookups")
Set ftime_kron = kron_wbk.Sheets("FullTime")

Set driver = SeleniumVBA.New_WebDriver
Set keys = SeleniumVBA.New_WebKeyboard
Set p_vars = ActiveWorkbook.Worksheets("PageVars")

driver.StartEdge

Set caps = driver.CreateCapabilities(False)

caps.AddArguments "--window-size=1920,1080"

caps.initializeFor Edge

driver.OpenBrowser caps, False

driver.Wait 2000

' Find Edge window handle
edgeWindow = FindWindowA("MicrosoftEdgeWindowClass", vbNullString)

' Resize Edge window (adjust dimensions as needed)
SetWindowPos edgeWindow, 0, 0, 0, 1200, 800, &H2 'SWP_FRAMECHANGED Or SWP_NOMOVE`

I've been digging deeper on how to change the default for msedgedriver but I got no luck in finding out. It would be really useful to know if there is a way to change the default settings. But if not I guess I would have to result in scrolling the window to find each element.

GCuser99 commented 7 months ago

Try removing the caps.initializeFor Edge as that is never needed (it's a private/friend procedure). By calling it after the caps.AddArguments "--window-size=1920,1080" you effectively reset the caps object and nullified the argument setting.

Your code in previous message should work with that change (did on my system).

Mike

GCuser99 commented 7 months ago

In addition to using the --window-size argument, you could also consider caps.AddArguments "--start-maximized". Tested on my system and that works too.

After you get the capabilities working like you want to, then run the following to create the pre-loaded capabilities json file:

'set all of your "default" capabilities here:
caps.AddArguments "--window-size=1920,1080"
'...
'...

'now save capabilities to a json file path of your choice
caps.SaveToFile "%USERPROFILE%\Documents\SeleniumVBA\capabilities\edge.json"

'now create the SeleniumVBA.ini file if you don't have one already (will be placed in
'same folder as your SeleniumVBA code lib)
driver.CreateSettingsFile keepExistingValues:=True

Then, edit the below portion of the ini file to point to the location of edge.json created above:

[EDGE]

preload_capabilities_file_path=%USERPROFILE%\Documents\SeleniumVBA\capabilities\edge.json
local_host_port=9516

If you did all of that correctly, then you won't need to use the WebCapabilities object any longer to set the window size. Each time you call driver.OpenBrowser those capabilities will be loaded automatically. Remember that the SeleniumVBA.ini file must be located in the same folder as your SeleniumVBA code library.

Good luck!

Vesuvex commented 7 months ago

Hi Mike,

Thank you so much for the help. I appreciate it. It turns out the solution was to set the profile to the chrome profile instead of edge and I was able to control the window from there. I understand the above solutions would've worked in my own laptop and apologies if I hadnt disclosed that the laptop I use have a lot of restrictions including updating edgedriver.

I can share the final code as a solution for others if they encounter the same issue.

Please let me know if anyone wants it.

Regards, Vesuvex

GCuser99 commented 7 months ago

Ok - happy that you sorted this out. I'm going to convert this to a discussion since it's really not an issue with SeleniumVBA, and so that others can refer to it if needed...

Mike