【问题标题】:Use default Chrome profile with Selenium in Python在 Python 中使用带有 Selenium 的默认 Chrome 配置文件
【发布时间】:2019-05-27 06:54:04
【问题描述】:

当我像这样使用 Selenium 启动 Chrome 时...

from selenium import webdriver
driver = webdriver.Chrome("/path/to/chromedriver")

...我得到了一个“干净”的 Chrome 实例,没有浏览历史记录,也没有我在计算机上正常安装 Chrome 时存储的任何数据。

我想用 Selenium 打开 Chrome,但我的默认 Chrome 安装中的书签、历史记录、cookie、缓存等在打开的浏览器中可用。

我该怎么做?

【问题讨论】:

    标签: python selenium google-chrome


    【解决方案1】:

    您可以使用特定配置文件或问题中的默认配置文件:

    如何在 Python 中使用 Selenium 在 Chrome 中打开一个新的默认浏览器窗口?

    这是一个设置的片段:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_argument("--user-data-dir=THE/PATH/TO/YOUR/PROFILE") # change to profile path
    chrome_options.add_argument('--profile-directory=Profile 1')
    
    browser = webdriver.Chrome(executable_path="PATH/TO/cromedriver.exe", chrome_options=chrome_options) # change the executable_path too
    

    要查找配置文件的路径,只需在您的默认 chrome 浏览器中键入 chrome://version/,您将在 配置文件路径: 下看到它:在我的 PC 中它是 "C:\Users\user\AppData\Local\Google\Chrome\User Data\Default"

    希望对您有所帮助!

    【讨论】:

    • 感谢您的建议。但它没有按我的意愿工作。它没有显示我的默认浏览器的书签和历史记录。请进一步提出建议。
    • 您可以使用您的个人资料...不是默认...我将添加到公众的答案...
    • @BirajRaj 我已将此添加到答案中:chrome_options.add_argument('--profile-directory=Profile 1')
    • @BirajRaj 我仍然认为编辑你的问题是个好主意......这可以帮助其他人解决这个问题......
    【解决方案2】:

    如果您只是想打开一个新的浏览器,您可以使用 Selenium 创建一个新的驱动程序并连接 Chrome。您需要从here 下载chrome 驱动程序,并将其放在如下所示的路径中。

    这是python代码:

    from selenium import webdriver
    
    driver = webdriver.Chrome("path-to-chromedriver")
    driver.get("https://www.google.com/")
    

    【讨论】:

    • 我想在我的默认浏览器(Chrome)中打开浏览器窗口。
    猜你喜欢
    • 1970-01-01
    • 2021-03-12
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多