51Testing软件测试论坛

标题: selenium模块添加cookie总是报错 [打印本页]

作者: 测试积点老人    时间: 2022-1-20 09:59
标题: selenium模块添加cookie总是报错
使用selenium模块登录163网易邮箱,在添加cookie的时候总是报各种各样的错误,比如:missing "cookie",mimatch"domain",等等。求解决
  1. from  selenium import webdriver
  2. from selenium.webdriver import ChromeOptions
  3. import time
  4. import pickle

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

作者: qqq911    时间: 2022-1-21 10:19
检查下报文头
作者: kallinr    时间: 2022-1-21 15:39
看报头




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2