Home > database >  Is there a way to use selenium in VBA, get the url, and not open the browser?
Is there a way to use selenium in VBA, get the url, and not open the browser?

Time:10-27

I have looked around and have not found a way using get in VBA within excel with selenium. I am trying not open the the browser and still get the url info. Does anyone know a way using selenium in vba to do this? For example:


Option Explicit

Sub TestSelenium()
    
    dim MyBrowser As Selenium.ChromeDriver

    Set MyBrowser = New Selenium.ChromeDriver
    
    MyBrowser.Start
    
    driver.Get "https://www.google.com/"

End Sub

I have seen way such as with the html object library

Option Explicit

Public Sub test()
    'tools > references > Microsoft HTML Object Library
    Dim html As MSHTML.HTMLDocument, xhr As Object
    
    Set xhr = CreateObject("MSXML2.XMLHTTP")
    Set html = New MSHTML.HTMLDocument

    With xhr
        .Open "GET", ""https://www.google.com/"", False
        .setRequestHeader "User-Agent", "Safari/537.36"
        .Send
        html.body.innerHTML = .responseText
        Debug.Print html.Title
    End With
    

End Sub

However, I am interested in solving the problem using selenium.

CodePudding user response:

Please use headless mode for this use case.

Dim driver As New ChromeDriver, post As Object

With driver
        .AddArgument "--headless"   ''This is the fix
        .get "https://yts.am/browse-movies"
    End With

CodePudding user response:

download

https://phantomjs.org/download.html

and add the exe to the correct file path in selenium


Sub HeadlessSelenium()
    
    Dim PJSD As Selenium.PhantomJSDriver
    Dim strHTML As String

    ' Instantiate Selenium through the PhantomJS Driver
    Set PJSD = New Selenium.PhantomJSDriver
    PJSD.Start
    
    ' Navigate to the URL
    PJSD.Get "https://www.google.com/"

    ' Extract the HTML code of the website
    strHTML = PJSD.PageSource
    
    ' Print the HTML code to the Immediate Window
    Debug.Print strHTML

End Sub

  • Related