测试积点老人 发表于 2022-1-20 09:59:27

selenium模块添加cookie总是报错

使用selenium模块登录163网易邮箱,在添加cookie的时候总是报各种各样的错误,比如:missing "cookie",mimatch"domain",等等。求解决
fromselenium import webdriver
from selenium.webdriver import ChromeOptions
import time
import pickle

option = ChromeOptions()
option.add_experimental_option("excludeSwitches",["enable-automation"])
bro = webdriver.Chrome(executable_path=r'C:\Users\Administrator\Desktop\chromedriver.exe')
bro.get("https://mail.163.com/")
bro.implicitly_wait('2.3')
# bro.find_element(by='xpath',value='//*[@id="account-box"]')
src = bro.find_element_by_tag_name('iframe')
bro.switch_to.frame(src)
bro.find_element_by_xpath('/html/body/div/div/div/form/div/div/div/input').send_keys('***')
bro.find_element_by_xpath('/html/body/div/div/div/form/div/div/div/input').send_keys('***')
bro.find_element_by_xpath('/html/body/div/div/div/form/div/div/a').click()
cookies = bro.get_cookies()
with open('./163cookies.txt','wb') as fp:
    pickle.dump(cookies,fp)
with open('./163cookies.txt','rb') as fp:
    cookies =pickle.load(fp)
bro.get('https://mail.163.com/')
bro.delete_all_cookies()
bro.refresh()
time.sleep(5)
for cookie in cookies:
    bro.add_cookie(cookie)
bro.refresh()Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/test.py", line 31, in <module>
    bro.add_cookie(cookie)
File "D:\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1124, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
File "D:\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
    self.error_handler.check_response(response)
File "D:\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain: Cookie 'domain' mismatch
(Session info: chrome=97.0.4692.71)
我尝试删除domain,并添加也不行.
求解答

qqq911 发表于 2022-1-21 10:19:35

检查下报文头

kallinr 发表于 2022-1-21 15:39:34

看报头
页: [1]
查看完整版本: selenium模块添加cookie总是报错