selenium3+python3—操作cookie
webdriver操作cookie的方法如下:
get_cookies()
get_cookie(name)
add_cookie(cookie_dict)
delete_cookie(name,optionsString)
delete_cookies()
参考代码:
from selenium import webdriver import time driver = webdriver.Chrome() driver.get('https://www.baidu.com') file = open('./data/fds.txt', 'rt') # 用txt文件进行参数化 lines = file.readlines() print(lines) file.close() for line in lines: nn = line[:-1] print(nn) driver.find_element_by_id('kw').clear() print(line.split()) driver.find_element_by_id('kw').send_keys(nn) driver.find_element_by_id('su').click() time.sleep(3) print(driver.current_url) cookies = driver.get_cookies() # 获取所有cookie print(cookies) cookie = driver.get_cookie(name='aaa') # 获取key(name)为aaa的cookie print(cookie) driver.add_cookie({'name':'aaa', 'value':'bbb'}) # 添加cookie driver.delete_cookie(name='aaa') # 删除名为aaa的cookie driver.delete_all_cookies() # 删除所有cookie driver.quit()
结果:
['aaa\n', 'bbb\n', 'ccc\n']
aaa
['aaa']
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=aaa&fenlei=256&rsv_pq=f3cc37fb00049c97&rsv_t=4768STDxLxXJ6Um5MMUVn2WLlFHXQVdL3CSGMaxdYkVBBNwJeKpbM7vjKu8&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=3&rsv_btype=i&inputT=116&rsv_sug4=116
[{'domain': 'www.baidu.com', 'expiry': 1620287303, 'httpOnly': False, 'name': 'COOKIE_SESSION', 'path': '/', 'secure': False, 'value': '0_0_1_0_0_1_0_0_0_1_2_0_0_0_0_0_0_0_1588751302%7C1%230_0_1588751302%7C1'}, {'domain': 'www.baidu.com', 'httpOnly': False, 'name': 'BDSVRTM', 'path': '/', 'secure': False, 'value': '154'}, {'domain': 'www.baidu.com', 'expiry': 1588753895, 'httpOnly': False, 'name': 'H_PS_645EC', 'path': '/', 'secure': False, 'value': '956e94oX3U%2BT4Np%2Bg9Td4tduvYEZXaMh2eHVM7SskQ49rucIhRpa5bQH6vE'}, {'domain': 'baidu.com', 'httpOnly': False, 'name': 'PSINO', 'path': '/', 'secure': False, 'value': '6'}, {'domain': 'baidu.com', 'expiry': 1588837703.359603, 'httpOnly': False, 'name': 'BDORZ', 'path': '/', 'secure': False, 'value': 'B490B5EBF6F3CD402E515D22BCDA1598'}, {'domain': 'baidu.com', 'httpOnly': False, 'name': 'delPer', 'path': '/', 'secure': False, 'value': '0'}, {'domain': 'baidu.com', 'httpOnly': False, 'name': 'H_PS_PSSID', 'path': '/', 'secure': False, 'value': '1455_21078_31425_31341_31464_30824_31163'}, {'domain': 'baidu.com', 'expiry': 1620287301.144514, 'httpOnly': False, 'name': 'BAIDUID', 'path': '/', 'secure': False, 'value': 'D7F28B5918D79DFEF031ADDC6A9413F1:FG=1'}, {'domain': 'baidu.com', 'expiry': 3736234948.144477, 'httpOnly': False, 'name': 'BIDUPSID', 'path': '/', 'secure': False, 'value': 'D7F28B5918D79DFEE7A9EBCE615D8BC4'}, {'domain': 'www.baidu.com', 'expiry': 1589615301, 'httpOnly': False, 'name': 'BD_UPN', 'path': '/', 'secure': False, 'value': '12314753'}, {'domain': 'www.baidu.com', 'httpOnly': False, 'name': 'BD_CK_SAM', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'www.baidu.com', 'httpOnly': False, 'name': 'BD_HOME', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'baidu.com', 'expiry': 3736234948.144496, 'httpOnly': False, 'name': 'PSTM', 'path': '/', 'secure': False, 'value': '1588751300'}]
None
[]
bbb
['bbb']
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=bbb&fenlei=256&oq=aaa&rsv_pq=9cf79f8c00062d71&rsv_t=956e94oX3U%2BT4Np%2Bg9Td4tduvYEZXaMh2eHVM7SskQ49rucIhRpa5bQH6vE&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t&inputT=68&rsv_sug3=6&rsv_sug4=68
[{'domain': 'www.baidu.com', 'httpOnly': False, 'name': 'BDSVRTM', 'path': '/', 'secure': False, 'value': '138'}, {'domain': 'baidu.com', 'httpOnly': False, 'name': 'PSINO', 'path': '/', 'secure': False, 'value': '7'}, {'domain': 'www.baidu.com', 'expiry': 1588753897, 'httpOnly': False, 'name': 'H_PS_645EC', 'path': '/', 'secure': False, 'value': '473d49y6tvG7J1lnhkANhQ4kDhfy6tuO33274h2qnV78c55pjaFpoKp1f6o'}, {'domain': 'baidu.com', 'expiry': 2597903305, 'httpOnly': False, 'name': 'BIDUPSID', 'path': '/', 'secure': False, 'value': 'D7F28B5918D79DFEE7A9EBCE615D8BC4'}, {'domain': 'baidu.com', 'httpOnly': False, 'name': 'H_PS_PSSID', 'path': '/', 'secure': False, 'value': '1426_31326_21081_31423_31341_31463_30823_26350_31164_31472'}, {'domain': 'baidu.com', 'expiry': 1620287305.622, 'httpOnly': False, 'name': 'BAIDUID', 'path': '/', 'secure': False, 'value': 'CB8B888E9D2401BD1DDFE963D0A06B97:FG=1'}, {'domain': 'www.baidu.com', 'httpOnly': False, 'name': 'BD_CK_SAM', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'baidu.com', 'expiry': 1588837705.905821, 'httpOnly': False, 'name': 'BDORZ', 'path': '/', 'secure': False, 'value': 'B490B5EBF6F3CD402E515D22BCDA1598'}, {'domain': 'baidu.com', 'httpOnly': False, 'name': 'delPer', 'path': '/', 'secure': False, 'value': '0'}, {'domain': 'baidu.com', 'expiry': 3736234952.621979, 'httpOnly': False, 'name': 'PSTM', 'path': '/', 'secure': False, 'value': '1588751305'}]
None
[]
ccc
['ccc']
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=ccc&fenlei=256&oq=bbb&rsv_pq=bd0444bb0004ff54&rsv_t=473d49y6tvG7J1lnhkANhQ4kDhfy6tuO33274h2qnV78c55pjaFpoKp1f6o&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t&inputT=74&rsv_sug3=9&rsv_sug4=74
[{'domain': 'www.baidu.com', 'httpOnly': False, 'name': 'BDSVRTM', 'path': '/', 'secure': False, 'value': '774'}, {'domain': 'baidu.com', 'httpOnly': False, 'name': 'PSINO', 'path': '/', 'secure': False, 'value': '7'}, {'domain': 'www.baidu.com', 'expiry': 1620287309, 'httpOnly': False, 'name': 'COOKIE_SESSION', 'path': '/', 'secure': False, 'value': '7_0_1_0_0_1_0_0_0_1_1_0_0_0_0_0_0_0_1588751309%7C2%230_0_1588751309%7C1'}, {'domain': 'www.baidu.com', 'expiry': 1588753901, 'httpOnly': False, 'name': 'H_PS_645EC', 'path': '/', 'secure': False, 'value': 'b311r3ED0%2BKw2z5Icu3Q%2BkYJhymfQvu5lwebZrhcCEvHZoriqeDymBkMsF8'}, {'domain': 'baidu.com', 'expiry': 2597903309, 'httpOnly': False, 'name': 'BIDUPSID', 'path': '/', 'secure': False, 'value': 'D7F28B5918D79DFEE7A9EBCE615D8BC4'}, {'domain': 'baidu.com', 'httpOnly': False, 'name': 'H_PS_PSSID', 'path': '/', 'secure': False, 'value': '31360_1465_31124_21089_31424_31341_31271_31464_31228_30823_26350_31163'}, {'domain': 'baidu.com', 'expiry': 1620287308.99033, 'httpOnly': False, 'name': 'BAIDUID', 'path': '/', 'secure': False, 'value': '663B5147DEB71561B01072D10DD9ECEF:FG=1'}, {'domain': 'www.baidu.com', 'httpOnly': False, 'name': 'BD_CK_SAM', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'baidu.com', 'expiry': 1588837709.25021, 'httpOnly': False, 'name': 'BDORZ', 'path': '/', 'secure': False, 'value': 'B490B5EBF6F3CD402E515D22BCDA1598'}, {'domain': 'baidu.com', 'httpOnly': False, 'name': 'delPer', 'path': '/', 'secure': False, 'value': '0'}, {'domain': 'baidu.com', 'expiry': 3736234955.990302, 'httpOnly': False, 'name': 'PSTM', 'path': '/', 'secure': False, 'value': '1588751308'}]
None