0
from selenium import  webdriver

option = webdriver.ChromeOptions()
option.add_argument('--start-maximized')  # 最大化
option.add_argument('--headless')  # 无界面
driver = webdriver.Chrome(chrome_options=option)
cookie = {}

cookies = 'zg_did=%7B%22did%22%3A%20%221638da685ff8d-0f51b2a0e616b4-4446062d-100200-1638da686001b4%22%7D; UM_distinctid=1638da6869c1ff-091ef411638c34-4446062d-100200-1638da686bd388; _uab_collina=152708989125868625311999; _umdata=BA335E4DD2FD504FAD5C66AE3D1DE79C3C52D36FC51589034C65468A14DB7240C207DEBA74760776CD43AD3E795C914CC7CA9E956ECE73C084D33288E55230E2; PHPSESSID=fr08prddt7p9sj5gbglo5cf1u4; acw_tc=AQAAAB6lll13bAgAzB38ckPCGVASetgi; hasShow=1; CNZZDATA1254842228=1675860325-1527086207-https%253A%252F%252Fwww.baidu.com%252F%7C1528906274; zg_de1d1a35bfa24ce29bbf2c7eb17e6c4f=%7B%22sid%22%3A%201528906426817%2C%22updated%22%3A%201528907862710%2C%22info%22%3A%201528899684702%2C%22superProperty%22%3A%20%22%7B%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22www.baidu.com%22%7D; Hm_lvt_3456bee468c83cc63fb5147f119f1075=1528904017,1528906427,1528906440,1528907863; Hm_lpvt_3456bee468c83cc63fb5147f119f1075=1528907863'

for line in cookies.split(';'):
    key,value = line.split('=', 1) #1代表只分一次,得到两个数据
    cookie[key] = value
print (cookie)

driver.get("https://www.qichacha.com/")
driver.add_cookie(cookie)
driver.find_element_by_id("searchkey").send_keys("baidu")
driver.find_element_by_id("V3_Search_bt").click()
print(driver.page_source)



  Traceback (most recent call last):
  File "headless.py", line 17, in <module>
    driver.add_cookie(cookie)
  File "/usr/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 874, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
  File "/usr/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3.4/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid argument: missing 'name'
  (Session info: headless chrome=66.0.3359.181)
  (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Linux 3.10.0-514.26.2.el7.x86_64 x86_64)



1个回答

0

cookie至少需要name和value两个key。

可以参考官方的文档:
http://selenium-python.readth...

撰写答案