Home > Software engineering >  Selenium Zoom with VBA
Selenium Zoom with VBA

Time:05-24

I am looking for a way to control Zoom in chrome with selenium.

I have tried

driver.ExecuteScript ("document.body.style.zoom='75%'") 

This zoom in the actual page, NOT like ctrl & -

driver.ExecuteScript "chrome.settingsPrivate.setDefaultZoom(1.5)"
  • This wont complie

I have no problems with eg. CTRL a (for selecting evrything

driver.FindElementByTag("body").SendKeys Keys.Control, "a"

but it dosn't work for SendKeys Keys.Control, Subtract

CodePudding user response:

The second command doesn't compile because before using it you have to go to the settings page

driver.Get('chrome://settings/')
driver.ExecuteScript('chrome.settingsPrivate.setDefaultZoom(1.5);')

CodePudding user response:

Login Zoom and open multiple pages.

First- dont Zoom before login, it wont click the BTN, heres my final code

Dim driver As New Selenium.ChromeDriver ' defines outside sub to prevent Crome to close at end of sub
sub loginOpen() 
    driver.start "chrome", "" ' new driver instance
    Dim sURL As String
    Dim rng As Range
    Dim First As Boolean
    Dim i As Variant
    First = True
    Set rng = Selection
    With driver
        For Each i In rng
            If First Then
                .Get sURL ' could also just be the login page
                .Window.SetSize 1400, 768
                First = False
                    ' if we need to login
                If Left(.Url, Len(sURL)) <> sURL Or .Url = "" Then
                    .FindElementByName("tbusername").SendKeys ("user")
                    .FindElementByName("tbpassword").SendKeys ("password")
                    .FindElementById("btnlogin").Click
                    .Get ("chrome://settings/")
                    .ExecuteScript ("chrome.settingsPrivate.setDefaultZoom(0.75);")
                    .Get sURL
                End If
            Else
                .ExecuteScript "window.open(arguments[0])", sURL
                .SwitchToNextWindow
            End If
        Next
    End With
End Sub
  • Related