51Testing软件测试论坛
标题:
selenium模块添加cookie总是报错
[打印本页]
作者:
测试积点老人
时间:
2022-1-20 09:59
标题:
selenium模块添加cookie总是报错
使用selenium模块登录163网易邮箱,在添加cookie的时候总是报各种各样的错误,比如:missing "cookie",mimatch"domain",等等。求解决
from selenium 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[2]/div[2]/div[2]/form/div/div[1]/div[2]/input').send_keys('***')
bro.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[3]/div[2]/input[2]').send_keys('***')
bro.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[8]/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
检查下报文头
作者:
kallinr
时间:
2022-1-21 15:39
看报头
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2