TypeError: WebDriver.get() got an unexpected keyword argument 'headers'
时间: 2023-11-20 11:05:15 浏览: 42
这个错误通常是因为WebDriver.get()方法不支持headers参数。如果你需要设置请求头,可以使用selenium.webdriver.remote.webdriver.WebDriver.execute()方法来发送自定义请求。你可以使用以下代码来设置请求头:
```
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# 设置请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
}
# 创建一个DesiredCapabilities对象
capabilities = DesiredCapabilities.CHROME.copy()
capabilities['acceptSslCerts'] = True
capabilities['acceptInsecureCerts'] = True
capabilities['loggingPrefs'] = {'browser': 'ALL'}
# 将请求头添加到DesiredCapabilities对象中
capabilities['chrome.page.customHeaders'] = headers
# 创建一个ChromeDriver实例,并将DesiredCapabilities对象传递给它
driver = webdriver.Chrome(desired_capabilities=capabilities)
# 使用get()方法访问网页
driver.get('https://www.example.com')
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)