How to open chrome with extension using selenium V

2019-09-21 03:01发布

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 ..

1条回答
女痞
2楼-- · 2019-09-21 03:33

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:\Users\User\AppData\Local\Google\Chrome\User Data\Profile 1"
    Const JS_PROFILE As String = "C:\Users\User\AppData\Local\Google\Chrome\User Data\Default"
    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:\Users\florent\Downloads\Personal-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:\Users\florent\Downloads\firebug-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:\Users\User\AppData\Local\Temp\Selenium\scoped_dir6268_2742\Default"
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
'------------------------------------------------
查看更多
登录 后发表回答