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
322 views
in Technique[技术] by (71.8m points)

chromedriver wont get website python selenium

I want my program to open up the default chrome profile and then get youtube. I can make it either open youtube (in a new chrome browser), or open the default chrome profile, but not both. (And no im not running both driver variables)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time


print('starting')
print('getting driver')


exec_path= "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"               # exec path from chrome://version

profilePath= 'C:\Users\MyName\AppData\Local\Google\Chrome\User Data\Profile 1'      #profile path from chrome://version

chromePath= 'C:\Users\MyName\OneDrive\Documents\Python programming\chromedriver.exe'  #path to driver



options= webdriver.ChromeOptions()

options.add_argument(profilePath)
print('options add argument...')

### Run one or the other ###
driver = webdriver.Chrome(executable_path= chromePath , options=options) #gets youtube
driver = webdriver.Chrome(executable_path= exec_path, options=options) #gets chrome profile


print('webdriver getting youtube...')
driver.get("https://www.youtube.com/")

when I run the driver line that gets the chrome profile I get the error:

Traceback (most recent call last): File "C:UsersMyNameOneDriveDocumentsPython programmingWeb automationwebAuto.py", line 24, in driver = webdriver.Chrome(executable_path= exec_path, options=options) #gets chrome profile File "C:UsersMyNameAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverchromewebdriver.py", line 73, in init self.service.start() File "C:UsersMyNameAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdrivercommonservice.py", line 104, in start raise WebDriverException("Can not connect to the Service %s" % self.path) selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:Program Files (x86)GoogleChromeApplicationchrome.exe

question from:https://stackoverflow.com/questions/65863395/chromedriver-wont-get-website-python-selenium

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

1 Reply

0 votes
by (71.8m points)

As per your question and your code trials if you want to open a Chrome with the customized Chrome Profile, Try using below code.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('C:\Users\MyName\AppData\Local\Google\Chrome\User Data\Profile 1' )
driver = webdriver.Chrome(executable_path='C:\Users\MyName\OneDrive\Documents\Python programming\chromedriver.exe', chrome_options=options)
driver.get("https://www.youtube.com/")

Here you will find a detailed discussion on How to open a Chrome Profile through Python


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

...