Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriverException when starting chromedriver with user-data-dir argument

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?

like image 811
Olexiy Avatar asked Jan 03 '23 21:01

Olexiy


1 Answers

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)
like image 108
Giannis Avatar answered Jan 05 '23 10:01

Giannis