Python selenium 使用 cookie

Python selenium using cookies

你好,我想问 chromedriver。我想要我自己的历史记录、cookies 和我原来的 chrome 浏览器中的所有其他东西。如果这不可能,如何使用原始 chrome 浏览器代替 driver?

您可以导入默认的 Chrome 浏览器配置文件以供 Selenium Chrome 驱动程序使用,如下所示:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\yourUserFolderName\AppData\Local\Google\Chrome\User Data\Default")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)

您可以使用 chrome 配置文件(请参阅下图以确定要启动哪个配置文件)

  1. 默认配置文件:

代码:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\***\AppData\Local\Google\Chrome\User Data")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options=options)
driver.get("https://www.google.co.in")
  1. 对于自定义配置文件:

代码:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\***\AppData\Local\Google\Chrome\User Data")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options=options)
driver.get("https://www.google.co.in")

您可以在 运行 chrome://version/ 在 Google chrome 中获取个人资料。