Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - Selenium not using Default Chrome Profile

Selenium will not load my default Chrome Profile and I cannot figure out why. I have tried both Profile 1 and a Default profile with the same error (below). I have confirmed with Task Manager that all Chrome windows are shut down before running this code. Any thoughts?

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import os
os.system("taskkill /f /im geckodriver.exe /T")
os.system("taskkill /f /im chromedriver.exe /T")
os.system("taskkill /f /im IEDriverServer.exe /T")
os.system("taskkill /f /im chrome.exe /T")

driver2 = r"C:Usersxxx.wdmdriverschromedriver87.0.4280.20win32chromedriver.exe"
options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\Usersxxx\AppData\Local\Google\Chrome\User Data\Profile 1")
driver = webdriver.Chrome(executable_path=driver2, chrome_options=options)
driver.get("https://www.google.co.in")

Traceback (most recent call last):
  File "C:UsersxxxOneDrivePythonpyReportRun.py", line 16, in <module>
    driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
  File "C:Python38libsite-packagesseleniumwebdriverchromewebdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "C:Python38libsite-packagesseleniumwebdriver
emotewebdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:Python38libsite-packagesseleniumwebdriver
emotewebdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:Python38libsite-packagesseleniumwebdriver
emotewebdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:Python38libsite-packagesseleniumwebdriver
emoteerrorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Could not remove old devtools port file. Perhaps the given user-data-dir at C:UsersxxxAppDataLocalGoogleChromeUser DataProfile 1 is still attached to a running Chrome or Chromium process

enter image description here

enter image description here

enter image description here

[1]: https://i.stack.imgur.com/qf1Rr.png

enter image description here

enter image description here

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
chrom_options.add_argument("user-data-dir=C:\Users
obert.car\AppData\Local\Google\Chrome\User Data")

chrom_options.add_argument("profile-directory=Profile 1")

user-data-dir considers profile as default , and you don't have to specify that . If its something else specify it through profile-directory argument

Step to create a profile:

open : chrome://version in address bar

enter image description here

copy the user dir folder completely to eg c:mp ewdir

open the copied user data (newdir) and search for folder called Default . This is the profile folder.

rename the Default folder as "Profile 1"

Now to use this :

chrom_options.add_argument("user-data-dir=c:\tmp\newdir")

chrom_options.add_argument("profile-directory=Profile 1")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...