如何使用Selenium VBA打开带有扩展名的Chrome [英] How to open chrome with extension using selenium VBA

查看:312
本文介绍了如何使用Selenium VBA打开带有扩展名的Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程非常陌生,所以我想做的是扩展带有扩展功能的chrome.经过一番搜索后,我发现此链接: https://seleniumjava.com/2016/05/22/start-the-chrome-browser-with-extensions/amp/

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/

但是它谈论Java却毫无头绪...所以我想将相同的方法与VBA结合起来...预先感谢..

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

推荐答案

以下是我设置两个配置文件的结果:一个启用了javascript,另一个未启用.如果您已经在配置文件中安装了扩展名,则在通过正确的路径时应加载该扩展名.以我的经验,我发现创建配置文件并通过Selenium启动Chrome,然后添加扩展名更为可靠.例如,使用下面的脚本,我为其中一个配置文件打开了Chrome应用商店,并手动添加了Usersnap扩展程序.现在,当我再次启动此配置文件时,它就会出现.

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.

注意:我通过启动硒化铬并输入chrome://version/然后复制Profile Path以重新使用来设置配置文件.

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


这两个示例直接来自作者本人,可在GitHub上获得


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


上面显示了两种扩展加载方式(通过Load a Chrome Extension通过提供路径参数,创建Custom Chrome Profile并将路径传递给该方式).更多信息


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.

逐步完成设置温度配置文件(@ qharr和@YasserKhalil之间的对话)

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

这篇关于如何使用Selenium VBA打开带有扩展名的Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆