2

If any Chrome processes are running and the following code is run, Stack Overflow will NEVER load:

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\RvBVakama\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument('--profile-directory=Profile 1')
w = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe', options=options)

w.get("https://stackoverflow.com/")

If any Chrome processes exist and the following code is run, it will load Stack Overflow after a few moments:

w = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe')

w.get("https://stackoverflow.com/")

The only difference is that the first code block launches Chrome with profile 1.

Here is the error log from block one:

[13768:7324:1216/205746.092:ERROR:cache_util_win.cc(19)] Unable to move the cache: 0
[13768:7324:1216/205746.093:ERROR:cache_util.cc(140)] Unable to move cache folder C:\Users\RvBVakama\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache to C:\Users\RvBVakama\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GPUCache_000
[13768:7324:1216/205746.094:ERROR:disk_cache.cc(184)] Unable to create cache
[13768:7324:1216/205746.095:ERROR:shader_disk_cache.cc(622)] Shader Cache Creation failed: -2
Traceback (most recent call last):
  File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\ptvsd_launcher.py", line 45, in <module>
    main(ptvsdArgs)
  File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 265, in main
    wait=args.wait)
  File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 256, in handle_args
    run_main(addr, name, kind, *extra, **kwargs)
  File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 52, in run_main
    runner(addr, name, kind == 'module', *extra, **kwargs)
  File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\runner.py", line 32, in run
    set_trace=False)
  File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "c:\Users\RvBVakama\Desktop\tet.py", line 9, in <module>
    w = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe', options=options)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location C:\Program Files (x86)\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 6.1.7601 SP1 x86_64)

Notice the last line.

I am unsure as to why chromedriver assumes Chrome has crashed.

RvBVakama
  • 91
  • 1
  • 8
  • I assume profile 1 was the default user profile and your Chrome process is using it. So why do you want use the same profile as your normal chrome using? -- Why not create a new profile, or better still assign a different user-data-dir for testing/scraping? – Til Dec 16 '18 at 10:38
  • When the profile folder is being used by chrome, there will be some IO conflicts if you try to use chromedriver to load the profile. I've encountered this before, but didn't look into it. Just assigned different user-data-dir and things went fine. -- If you want all the settings from that default user, you can copy the folder to another place to use it afterwards. (Better close your chrome first) – Til Dec 16 '18 at 10:42
  • @POW, In fact, I have created another account for automation tasks. I am using that account over my default, I do this by specifying these arguments `options.add_argument("user-data-dir=C:\\Users\\RvBVakama\\AppData\\Local\\Google\\Chrome\\User Data") options.add_argument('--profile-directory=Profile 1')` The error is produced when any profile is loaded into Chrome (which is any time chrome is active, for me). – RvBVakama Dec 16 '18 at 10:51
  • @POW I kept reading your comments over and over and it kept trying things and I'm not sure if I am dumb, but is this what you were saying?: I just duplicated the "User Data" and renamed it to "User Data AUTO" , this has all my profiles, default, guest and profile 1. Now `options = webdriver.ChromeOptions() options.add_argument("user-data-dir=C:\\Users\\RvBVakama\\AppData\\Local\\Google\\Chrome\\User Data AUTO") options.add_argument('--profile-directory=Profile 1') w = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe', options=options)` This works! – RvBVakama Dec 16 '18 at 11:33
  • 1
    Glad you got it working :) – Til Dec 16 '18 at 12:07

2 Answers2

1

I'm not sure it is the same user-data-dir, but a different profile will lead to conflicts. I always use another user-data-dir with scripts.

-- But seeing your case I guess it is so.

Also BTW, to make life easier, why not just copy to some shorter directory address? --- say "C:\ChromeUserData".

And if you don't care about the old settings, you just specify user-data-dir to a new location. And Chrome will create the profile and things for you. (The directory specified needs to exist.)

For example:

options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=D:\\chromedriver\\UserDataDir") #Path to your chrome user-data/profile
options.add_argument('--disable-infobars') #disable the automation prompt bar
options.add_argument('--lang=en') #Set language to English
w = webdriver.Chrome(executable_path="D:\\chromedriver\\chromedriver.exe", chrome_options=options)

Given that D:\chromedriver\UserDataDir exists (or you create it first) and chromedriver.exe is at D:\chromedriver\chromedriver.exe.

By this way, you don't need to specify profile-directory any more. The script will create and use Default directory/profile inside D:\\chromedriver\\UserDataDir and save any changes you made.

Another tip: You can put chromedriver.exe in the directory of python.exe, thus to save the needs to specify executable_path, also will have fewer possible errors.

To make it more reliable, you can put these before above code snippets:

import os
uddPath = 'D:\\chromedriver\\UserDataDir'
if not os.path.exists(uddPath):
    os.makedirs(uddPath)
Til
  • 5,150
  • 13
  • 26
  • 34
1

Okay so the fix is to go to C:\Users\NAME\AppData\Local\Google\Chrome and duplicate your "User Data" folder and give it another name. Then just set your "user-data-dir" argument to the new folder.

# set up the chrome options to launch with the AUTO profile
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\RvBVakama\\AppData\\Local\\Google\\Chrome\\User Data AUTO")
options.add_argument('--profile-directory=Profile 1')
# start chrome with the driver
w = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe', options=options)

enter image description here

RvBVakama
  • 91
  • 1
  • 8
  • Sorry I have to point out: Duplicate "User Data" is for copy your old settings of chrome, and you can move the duplicated directory to elsewhere. But to solve this conflict problem you've encountered, just assign user-data-dir to another path would do. – Til Dec 16 '18 at 12:41
  • That is exactly what I did, I assigned the user-data-dir to another path. What am I not understanding? To my knowledge when assigning the user-data-dir path it must point to a folder within this hierarchal structure `*C:\\Users\\RvBVakama\\AppData\\Local\\Google\\Chrome\\*` anywhere else and it just returns errors as it cannot find any profiles in within a "User Data" folder. – RvBVakama Dec 16 '18 at 15:05
  • I have found out that I can delete all folders except profile 1 in the duplicate folder, run the script & chrome generates all needed folders n files. Maybe having another folder has something to do with chrome driver not getting confused as to which user was signed in last. I've noticed that when you have multiple accounts open, the account that opens next on the next launch is the last active one you had. Maybe the other folder having just the profile needed in it makes certain that chrome driver does not try to access the profile that may be open in the default user-data-dir path. – RvBVakama Dec 16 '18 at 15:16
  • Check above answer, you can assign any other existing path to user-data-dir, no need to be in that path structure you mentioned. It's important that the path is created before you used. But inside the user-data-dir it's no matter if it's empty or not. And you can left the profile parameter out, since the scripts will create and user Default profile inside the user-data-dir you've assigned. – Til Dec 16 '18 at 15:19
  • Well, I said "any other", but it sill needs to have proper fs permissions to be used. So usually it's better to use another drive like D: , or create a subfolder of C: first, thus the new folder has the read/write permissions for the scripts/chromedriver by default. – Til Dec 16 '18 at 15:45
  • The problem with that is yes I could leave the user-data-dir empty but that would, as you said, result in a default profile being created, one which has no cookies or login credentials. My script needs an account that had login information for certain websites. I understand the user-data-dir can be anywhere on the disk but I just think it is neat to keep it there. Before I figured out that I needed to use a profile with credentials already entered It worked fine as it was not using any profiles and no errors were generated. – RvBVakama Dec 17 '18 at 04:37
  • First time it's empty, but since you can interact with the webdriver issued chrome, you can login manually, It will save those credentials. And next time you run the same scripts, everything are still there. That's why I said "and save any changes you made." – Til Dec 17 '18 at 04:45
  • @POW ah yes that would work as well. My method of duplication and setting the user-data-dir to the new one which contained all the previous profiles is a bit weird now that I understand the process. However, it was just the way I ended up figuring it out as it is my real first project with python and this kind of tech. Thanks for all your help and I have enjoyed this conversation :) – RvBVakama Dec 17 '18 at 04:51