WebDriverException when starting chromedriver with user-data-dir argument

℡╲_俬逩灬. 提交于 2019-12-11 06:38:01

问题


My code:

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
opts = Options()
opts.add_argument("user-data-dir=/path/to/profiles_dir/user_id")
browser = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", chrome_options=opts)

When I start Chromium for user with id = 1 it starts fine and creates a profile directory /path/to/profiles_dir/1. Then I visit some arbitrary site and close the browser. When I execute the above code for the second time, it throws and exception.

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot parse internal JSON template: Line: 1, column: 1, Unexpected token. (Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.4.0-112-generic x86_64)

  • Chromium 64.0.3282.119 Built on Ubuntu , running on Ubuntu 16.04

  • ChromeDriver 2.35

  • selenium 3.8.1

I googled a lot but could not find a solution for this issue. Why can't I load the browser with the existing user profile dir? What am I doing wrong?


回答1:


There seems to be a bug in chromedriver. I narrowed it down, and two files seem to be the culprit: {user-data-dir}/Local State and {user-data-dir}/{profile-directory}/Preferences. If you do not specify profile-directory, it will be 'Default'.

Chrome/Chromium doesn't seem to be able to read these files, even if you properly close chromedriver, using browser.quit().

You'll need to delete the files in order to be able to start chromedriver again, using the same profile.

I used the following code in my finally block, in order to delete the files:

if browser is not None:
    browser.quit()
    time.sleep(1)
delete_paths = ['../selenium/chrome_profile/Local State',
                '../selenium/chrome_profile/Default/Preferences']
for delete_path in delete_paths:
    if os.path.exists(delete_path):
        os.remove(delete_path)


来源:https://stackoverflow.com/questions/48571886/webdriverexception-when-starting-chromedriver-with-user-data-dir-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!