【问题标题】:How to use Chrome Profile in Selenium Webdriver Python 3如何在 Selenium Webdriver Python 3 中使用 Chrome 配置文件
【发布时间】:2019-02-22 22:51:29
【问题描述】:

所以每当我尝试通过添加来使用我的 Chrome 设置(我在默认浏览器中使用的设置)时

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\... (my webdriver path)")
driver = webdriver.Chrome(executable_path="myPath", options=options)

它显示了错误代码

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes n 16-17: truncated \UXXXXXXXX escape

在我的 bash 中。我不知道这意味着什么,我会很高兴能得到任何帮助。提前致谢!

【问题讨论】:

    标签: python selenium google-chrome selenium-chromedriver chrome-profile


    【解决方案1】:

    根据您的问题和您的代码试验,如果您想打开 Chrome 浏览会话,可以使用以下选项:

    • 要使用默认的 Chrome 配置文件

      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      
      options = webdriver.ChromeOptions()
      options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
      driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
      driver.get("https://www.google.co.in")
      
    • 注意:您的默认 chrome 配置文件 将包含很多书签、扩展程序、主题、cookie 等。Selenium 可能无法加载它。因此,按照最佳实践,为您的 @Test 创建一个新的 chrome 配置文件,并在配置文件中存储/保存/配置所需的数据。

    • 要使用自定义的 Chrome 配置文件

      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      
      options = Options()
      options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
      driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
      driver.get("https://www.google.co.in")
      
    • 这里有关于How to open a Chrome Profile through Python的详细讨论

    【讨论】:

    • 我可以加载默认配置文件。但是一旦我添加了配置文件 2(我已经创建并现在出现在用户数据文件夹中),它将不会加载配置文件
    • 我无法让第二个配置文件以这种方式工作,必须按照此答案中的建议将其复制到其他位置:stackoverflow.com/a/66706022/10106315
    【解决方案2】:

    要获取路径,请按照以下步骤操作。

    在搜索栏中输入以下内容并按回车

    这将显示所有元数据。在那里找到配置文件的路径

    【讨论】:

      【解决方案3】:

      接受的答案是错误的。这是官方正确的做法:

      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      
      options = webdriver.ChromeOptions()
      options.add_argument(r"--user-data-dir=C:\path\to\chrome\user\data") #e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
      options.add_argument(r'--profile-directory=YourProfileDir') #e.g. Profile 3
      driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
      driver.get("https://www.google.co.in")
      

      要在 Windows 上找到配置文件文件夹,请右键单击要使用的 Chrome 配置文件的桌面快捷方式,然后转到属性 -> 快捷方式,您会在“目标”文本框中找到它。

      【讨论】:

      • 我应该费心保存cookie(例如通过pickle模块)还是加载配置文件意味着将它们保存在引擎盖下?
      • 你说的很对。非常感谢,即使主 google chrom 正在运行,我可以使用配置文件运行 webdriver 吗?因为我收到错误“selenium.common.exceptions.InvalidArgumentException:消息:无效参数:用户数据目录已在使用中,请为 --user-data-dir 参数指定唯一值,或者不要使用 --用户数据目录”
      【解决方案4】:

      您确定要在 user-data-dir 参数中放入 webdriver 路径吗?这通常是您放置 chrome 配置文件的位置,例如“C:\Users\yourusername\AppData\Local\Google\Chrome\User Data\Profile 1\”。此外,您需要在目录路径中使用双反斜杠或正斜杠(两者都有效)。您可以使用 os 库测试您的路径是否有效 例如

      import os
      os.list("C:\\Users\\yourusername\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1")
      

      会给你目录列表。

      我可能还会补充一点,如果您在使用指定的用户配置文件运行 webdriver 时设法使 chrome 崩溃,它似乎会在配置文件中记录崩溃,并且下次打开 chrome 时,您会收到 Chrome 提示以恢复页面异常退出后。就我个人而言,这有点让人头疼,因此我不再使用带有 chromedriver 的用户配置文件。我找不到解决办法。其他人在这里报告了它,但他们的解决方案似乎都不适合我,或者不适合我的测试用例。 https://superuser.com/questions/237608/how-to-hide-chrome-warning-after-crash 如果您不指定用户配置文件,它似乎每次运行时都会创建一个新的(空白)临时配置文件

      【讨论】:

        【解决方案5】:

        这就是我设法在 php selenium webdriver 中使用 EXISTING CHROME PROFILE 的方法。 配置文件 6 不是我的默认配置文件。我不知道如何运行默认配置文件。重要的是不要在 chrome 选项参数之前添加 -- !所有其他选项的变体都不起作用!

        <?php
        //...
        $chromeOptions = new ChromeOptions();
        $chromeOptions->addArguments([
            'user-data-dir=C:/Users/MyUser/AppData/Local/Google/Chrome/User Data',
            'profile-directory=Profile 6'
        ]);
        
        $host = 'http://localhost:4444/wd/hub'; // this is the default
        $capabilities = DesiredCapabilities::chrome();
        $capabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);
        $driver = RemoteWebDriver::create($host, $capabilities, 100000, 100000);
        

        要获取您的 chrome 配置文件的名称,请转到 chrome://settings/manageProfile,单击配置文件图标,单击“在我的桌面上显示配置文件快捷方式”。之后右键单击桌面配置文件图标并转到属性,在这里您将看到类似“C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”--profile-directory="Profile 6".

        另外,我建议您在运行此代码之前关闭所有 chrome 实例。此外,也许您需要关闭 chrome 设置 > 高级 > 系统 > “当 Google Chrome 关闭时继续运行后台应用程序”。

        【讨论】:

          【解决方案6】:

          确保您的配置文件路径正确,并且在所述路径中使用双转义反斜杠。

          例如,通常 Windows 上的默认配置文件位于:

          "C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default"

          【讨论】:

            【解决方案7】:

            给定的答案都不适合我,所以我研究了一下,现在工作代码就是这个。我从 chrome://version/ 的配置文件路径中复制了用户目录文件夹,并为配置文件创建了另一个参数,如下所示:

            from selenium import webdriver
            from selenium.webdriver.chrome.options import Options
            
            options = webdriver.ChromeOptions()
            options.add_argument('user-data-dir=C:\\Users\\gupta\\AppData\\Local\\Google\\Chrome\\User Data')
            options.add_argument('profile-directory=Profile 1')
            driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe', options=options)
            driver.get('https://google.com')
            

            【讨论】:

              【解决方案8】:

              我设法使用这些参数启动了我的 chrome 配置文件:

              ChromeOptions options = new ChromeOptions();
              options.addArguments("--user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data");
              options.addArguments("--profile-directory=Profile 2");
              WebDriver driver = new ChromeDriver(options);
              

              您可以了解更多关于网络驱动程序here

              【讨论】:

                猜你喜欢
                • 2021-05-15
                • 2018-09-02
                • 2015-09-12
                • 1970-01-01
                • 2020-06-14
                • 1970-01-01
                • 1970-01-01
                • 2013-01-06
                相关资源
                最近更新 更多