开启新的Chrome标签

时间:2019-04-13 14:04:55

标签: excel vba selenium web-scraping selenium-chromedriver

美好的一天。 通过打开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 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

'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