2

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.

4
  • 2
    You can use headless to hide the browser (but of course it is launched). Before the Start line, use this .AddArgument "--headless" Commented Oct 26, 2021 at 14:45
  • like this? MyBrowser..AddArgument "--headless" I wasn't able to get that to work, but it did point me to the correct path. Commented Oct 26, 2021 at 16:38
  • correction, a* correct path. Commented Oct 26, 2021 at 16:55
  • oh, I see now: driver.AddArgument "--headless" driver.Get "google.com" Commented Oct 26, 2021 at 17:43

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

This works and is more simple than what I ended up doing. Thank you
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.