当前位置: 首页 > 知识库问答 >
问题:

打开新的Chrome标签

空英逸
2023-03-14

很好的一天。请您通过打开Chrome浏览器创建一个新选项卡,在其中执行一些操作,关闭新选项卡,然后返回第一个选项卡并继续在其中工作,来判断Selenium VBA是否可行?

我用以下代码打开浏览器:

Public drv As New WebDriver

Public Sub browser_open()
Set drv= New WebDriver 'ChromeDriver
drv.Start "chrome", "https://google.com"
drv.Get "/"

End Sub

共有1个答案

吕奇
2023-03-14

你可以这样做:

'Ensure latest applicable driver e.g. ChromeDriver.exe in Selenium folder
'VBE > Tools > References > Add reference to selenium type library
Public Sub DownloadFile()
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const URL = "https://stackoverflow.com/"

    With d
        .Start "Chrome"
        .get URL
        .ExecuteScript "window.open('https://www.google.com/','_blank');"
        .SwitchToNextWindow
        'do something with new window
        Debug.Print .Window.Title
        .ExecuteScript "window.close();"
        .SwitchToPreviousWindow
        Debug.Print .Window.Title
        Stop
        .Quit
    End With
End Sub
 类似资料: