使用Selenium选择Chrome配置文件时出错

vaqhlq81  于 2023-02-12  发布在  其他
关注(0)|答案(1)|浏览(141)

我想打开一个新的Chrome驱动程序会话与选定的配置文件“配置文件1”。但我也想保持其他Chrome窗口/会话打开(其中有默认的配置文件)。
我尝试了这段代码,它正确地打开了新的Chrome窗口,并与“配置文件1”,因为我想要的。但我然后得到以下错误的VBA。

Private Sub Use_Chrome_With_Custom_profile_name()
  ' Profiles folder : %APPDATA%\Google\Chrome\Profiles
  ' Note that with Chrome the profile is always persistant

  Dim driver As New ChromeDriver

  driver.AddArgument "--user-data-dir=C:\Users\user1\AppData\Local\Google\Chrome\User Data"
  driver.AddArgument "--profile-directory=Profile 1"

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

如果我跳过(注解)这一行,那么Chrome不会打开配置文件1:

'driver.AddArgument "--user-data-dir=C:\Users\user1\AppData\Local\Google\Chrome\User Data"
db2dz4w8

db2dz4w81#

试试这个:

Dim driver As New WebDriver

    Const URL = "https://www.google.co.uk"
    Const JS_PROFILE As String = _
    "C:\Users\user1\AppData\Local\Google\Chrome\User Data\Profile 1"
    Set driver = New ChromeDriver
    With driver
        .SetProfile JS_PROFILE, True
        .Get URL
    End With

相关问题