Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
955 views
in Technique[技术] by (71.8m points)

excel - How to open chrome with extension using selenium VBA

I am very new to programming, so what I trying do is open chrome with extension. After doing some search i found this link : https://seleniumjava.com/2016/05/22/start-the-chrome-browser-with-extensions/amp/

However it talks about Java which I have no clue... So I want to incorporate the same method with VBA ... Thanks in advance ..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following is from when I set up two profiles: one with javascript enabled and the other without. If you have already got the extension installed in the profile it should load when you pass the correct path. In my experience I found it more reliable to create the profile and launch Chrome via Selenium and then add the extension. For example, with the script below I opened Chrome app store for one of my profiles and manually added Usersnap extension. Now, when I launch this profile again it is present.

Note: I set up profiles by launching selenium chrome and entering chrome://version/ then copying the Profile Path to re-use.

Option Explicit
Public Sub AddExtension()
    Dim d As WebDriver
    Const URL = "https://chrome.google.com/webstore/search/Usersnap"
    Const NO_JS_PROFILE As String = "C:UsersUserAppDataLocalGoogleChromeUser DataProfile 1"
    Const JS_PROFILE As String = "C:UsersUserAppDataLocalGoogleChromeUser DataDefault"
    Set d = New ChromeDriver
    With d
        .SetProfile JS_PROFILE, True   'NO_JS_PROFILE, True 
        .get URL
         Stop
        .Quit
    End With
End Sub

These two examples are direct from the author himself and available on GitHub

Private Sub Use_Chrome_With_Extension()
  ' To download an extension:
  ' http://chrome-extension-downloader.com
  ' To manage the extension preferences:
  ' Developper Tools > Resources > Local Storage > chrome-extension://...

  Dim driver As New ChromeDriver
  driver.AddExtension "C:UsersflorentDownloadsPersonal-Blocklist-(by-Google)_v2.6.1.crx"
  driver.SetPreference "plugins.plugins_disabled", Array("Adobe Flash Player")
  driver.Get "chrome-extension://nolijncfnkgaikbjbdaogikpmpbdcdef/manager.html"
  driver.ExecuteScript "localStorage.setItem('blocklist', '[""wikipedia.org""]');"

  driver.Get "https://www.google.co.uk"
  driver.Quit
End Sub


Private Sub Use_Firefox_With_Extension()
  ' To download an extension, use a browser other than Firefox

  Dim driver As New FirefoxDriver
  driver.AddExtension "C:UsersflorentDownloadsfirebug-2.0.12-fx.xpi"
  driver.SetPreference "extensions.firebug.showFirstRunPage", False

  driver.Get "https://www.google.co.uk"
  driver.Quit
End Sub

The above shows the 2 ways to load with extension (by Load a Chrome Extension by providing a path argument, creating a Custom Chrome Profile and passing the path to that. More info here.


Walk through of setting a temp profile (conversation between @qharr & @YasserKhalil)

'Run This Procedure 'GetInfo' First
'----------------------------------
Sub GetInfo()
Dim d As WebDriver

Set d = New ChromeDriver
Const URL = "https://pcsupport.lenovo.com/"

With d
    .Start "Chrome"
    .get URL
    Stop
End With
End Sub

'In The Browser Replace The Current URL With chrome://version/ And Press Enter
'Make A Note Of The Profile Path And Use It In This Procedure 'AddExtension'
'------------------------------------------------------------------------------
Sub AddExtension()
Dim d As WebDriver

Const MY_PROFILE As String = 
"C:UsersUserAppDataLocalTempSeleniumscoped_dir6268_2742Default"
Set d = New ChromeDriver
Const URL = "https://pcsupport.lenovo.com/"

With d
    .SetProfile MY_PROFILE, True
    .get URL
    Stop
    .Quit
End With
End Sub

'Now Navigate And Install Your Extension Manually
'------------------------------------------------

'Relaunch browser and extension should be present
'------------------------------------------------

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...