xuzhengyi1995 / Manga_downloader

A Manga download framework using selenium.
639 stars 58 forks source link

BUG report: add_cookie() throws UnableToSetCookieException #90

Open ulyssesric opened 2 years ago

ulyssesric commented 2 years ago

Error log:

When downloading from BookWalker I got this error:

Traceback (most recent call last):
  File "/usr/local/Manga_downloader/main.py", line 61, in <module>
    downloader.download()
  File "/usr/local/Manga_downloader/downloader.py", line 201, in download
    self.login()
  File "/usr/local/Manga_downloader/downloader.py", line 129, in login
    add_cookies(driver, self.cookies)
  File "/usr/local/Manga_downloader/downloader.py", line 41, in add_cookies
    driver.add_cookie({'name': i, 'value': cookies[i]})
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 1131, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 425, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.UnableToSetCookieException: Message: unable to set cookie
  (Session info: headless chrome=99.0.4844.51)

Cause of problem:

Chrome 99.0.4844.51 zapped one single whitespace between cookies when Copy Value in Developer Tool. The string should be: ...48422; krt.context=session:8699a48c-a1ab-11ec-b909-0242ac120002; context_mode:other; AMP_TOKEN...

but the copied value is ...48422; krt.context=session:8699a48c-a1ab-11ec-b909-0242ac120002;context_mode:other; AMP_TOKEN...

Note that the whitespace before context_mode is missing.

And line 26 of download.py split cookie with semicolon+whitespace: cookies_split = cookies.split('; ')

Manually add space in copied cookie string fixed this issue.

How to solve

Use semicolon without trailing whitespace when calling split, then trim the leading white space (if there is any) of cookie name.

xuzhengyi1995 commented 2 years ago

Will modify, thanks for providing the problem in such detail !