Python Selenium: 如何在Chrome浏览器中添加Cookie并确保生效
初始化Chrome浏览器
options = webdriver.ChromeOptions() options.add_argument('--disable-blink-features=AutomationControlled') # 禁用浏览器的自动化特性 options.add_experimental_option('excludeSwitches', ['enable-automation']) # 排除浏览器的自动化开关选项 options.add_experimental_option('useAutomationExtension', False) # 禁用浏览器的自动化扩展 #options.add_argument('--headless') # 无头模式,不显示浏览器窗口 options.add_argument('--no-sandbox') # 禁用沙盒模式 wd = webdriver.Chrome(options=options) # 按照上面的设置选项启动浏览器 wd.implicitly_wait(10)
打开网站
url = 'https://iam.pt.ouchn.cn/am/UI/Login?realm=%2F&service=initService&goto=https%3A%2F%2Fiam.pt.ouchn.cn%2Fam%2Foauth2%2Fauthorize%3Fservice%3DinitService%26response_type%3Dcode%26client_id%3D345fcbaf076a4f8a%26scope%3Dall%26redirect_uri%3Dhttps%253A%252F%252Fmenhu.pt.ouchn.cn%252Fouchnapp%252Fwap%252Flogin%252Findex%26decision%3DAllow/' wd.get(url) time.sleep(3) # 等待网站加载完成
添加cookie
cookie = {'name': 'my_cookie', 'value': 'my_value', 'domain': '.pt.ouchn.cn'} wd.add_cookie(cookie)
再次打开网站,确保cookie已生效
wd.get(url) time.sleep(3)
原文地址: https://www.cveoy.top/t/topic/jpAR 著作权归作者所有。请勿转载和采集!