import time
from webdriver_helper import webdriver, get_webdriver
from selenium.webdriver import chrome
from selenium.webdriver.common.by import By
driver = webdriver.Chrome() # 实例化 ,刚启动浏览器是空白页面
driver.maximize_window() # 窗口最大化
driver.get("http://118.24.147.95:8087/cookies/set/a/1") # 服务器会设置cookie
cookies = driver.get_cookies()#获取所有cookie
print(cookies)
data={'domain': '118.24.147.95', 'httpOnly': False, 'name': 'a', 'path': '/',
'secure': False, 'value': '2'}
driver.add_cookie(data)#添加或者修改cookie
cookies = driver.get_cookies()#获取所有cookie
print(cookies)
driver.refresh()#修改cookie 后刷新页面 看网站收到了什么新cookies
print(driver.page_source)#获取网页的内容
driver.quit() # 退出浏览器 调用对象的方法
执行结果1
[{'domain': '118.24.147.95', 'httpOnly': False, 'name': 'a', 'path': '/', 'secure': False, 'value': '1'}]
[{'domain': '118.24.147.95', 'httpOnly': False, 'name': 'a', 'path': '/', 'secure': False, 'value': '2'}]
<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{
"cookies": {
"a": "2"
}
}
</pre></body></html>
Process finished with exit code 0
代码2
import time
from webdriver_helper import webdriver, get_webdriver
from selenium.webdriver import chrome
from selenium.webdriver.common.by import By
driver = webdriver.Chrome() # 实例化 ,刚启动浏览器是空白页面
driver.maximize_window() # 窗口最大化
driver.get("http://118.24.147.95:8087/cookies/set/a/1") # 服务器会设置cookie
cookies = driver.get_cookies()#获取所有cookie
print(cookies)
data={'domain': '118.24.147.95', 'httpOnly': False, 'name': 'a', 'path': '/',
'secure': False, 'value': '2'}
driver.add_cookie(data)#添加或者修改cookie
driver.delete_cookie("a")
cookies = driver.get_cookies()#获取所有cookie
print(cookies)
driver.refresh()#修改cookie 后刷新页面 看网站收到了什么新cookies
print(driver.page_source)#获取网页的内容
driver.quit() # 退出浏览器 调用对象的方法
执行结果2
[{'domain': '118.24.147.95', 'httpOnly': False, 'name': 'a', 'path': '/', 'secure': False, 'value': '1'}]
[]
<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{
"cookies": {}
}
</pre></body></html>
Process finished with exit code 0
标签:webdriver,cookies,False,Selenium,读写,driver,cookie,import
From: https://www.cnblogs.com/cy-xt/p/16846304.html