给selenium中Chrome添加请求头,设置代理,添加cookie方法总结

一、添加header头

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36")
driver = webdriver.Chrome(chrome_options=chrome_options)

二、设置代理

# 复制一份内置的配置文件
desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()

# 改变配置文件的代理.
desired_capabilities['proxy'] = {
    "httpProxy": 代理ip,
    "ftpProxy": 代理ip,
    "sslProxy": 代理ip,
    "noProxy": None,
    "proxyType": "MANUAL",
    "class": "org.openqa.selenium.Proxy",
    "autodetect": False
}

driver = webdriver.Chrome(desired_capabilities=desired_capabilities)

三、添加单个cookie

driver = webdriver.Chrome()
driver.delete_all_cookies()
driver.add_cookie(cookie)

四、添加多个cookie 

driver = webdriver.Chrome()
driver.delete_all_cookies()
for cookie in cookies:
    driver.add_cookie(cookie)

 

你可能感兴趣的:(玩转python,selenium,python,cookie)