-2

This is my code. It opens Google Chrome but does not go to google.com:

from selenium import webdriver

path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
options = webdriver.ChromeOptions()
path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
options.add_argument(f'--user-data-dir={path}')
options.add_argument('--profile-directory=Default')
chrome_browser = webdriver.Chrome(executable_path=path, options=options)
chrome_browser.get('https://google.com/')

But if I use this, it opens Selenium's Chrome and works (no cache or cookie from my Google Chrome) (edit 1):

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

# path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
path = r'D:\Downloads\chromedriver_win32\chromedriver.exe'
options = webdriver.ChromeOptions()
user_data = r'C:\Users\Saeed\AppData\Local\Google\Chrome\User Data'
options.add_argument(f'--user-data-dir={user_data}')
options.add_argument('--profile-directory=Default')
driver = webdriver.Chrome(f'{path}', options=options)
driver.get('https://google.com')

Where's the issue?

Edit2

In fact when I use chromedriver.exe WITHOUT profile, it opens chrome driver with no history, passwords, etc.

But when I use chromedriver.exe WITH profile, it opens normal chrome but does not open the web page.

Saeed
  • 3,255
  • 4
  • 17
  • 36
  • You need ChromeDriver to automate chrome with selenium, I think you are trying to use normal chrome instance. You can load some user information in driver though. – Sandro J Mar 16 '22 at 22:36
  • @SandroJ yes I'm using normal Chrome. No, it does not answer me. It's still opening only new blank window – Saeed Mar 16 '22 at 22:49
  • 1
    You've said with webdriver it works (2nd code snippet), and you should do it this way and load the user information. That answer on the link means it's not possible to automate in normal chrome. – Sandro J Mar 16 '22 at 23:07
  • in the first code block you're giving a path to the chrome browser as the path to the chromedriver. webdriver.Chrome(executable_path=... this should be the path to the chromedriver. For setting a path to the chrome browser use options.setbinary... (I think it's option.binary_path in Python. You need to launch chromedriver. Chromedriver will launch Chrome. You send commands to chromedriver and it sends/receives commands to/from Chrome using the wire protocol. – pcalkins Mar 16 '22 at 23:49

1 Answers1

0

Selenium controls chromedriver.exe not chrome.exe. If you are wanting to use your default profile with Selenium, it needs to point to the directory not the executable itself. This is located usually within your AppData folder, like so:

path_to_profile = r'C:\\Users\\myuser\\AppData\\Local\\Google\\Chrome\\User Data'
path_to_executable =  r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
options.add_argument(f'--user-data-dir={path_to_profile}')
options.add_argument('--profile-directory=Default')
chrome_browser = webdriver.Chrome(executable_path=path_to_executable, options=options)
chrome_browser.get('https://google.com/')

Both paths would need to be declared. And remember to close your current running Chrome window before running , or else it will complain the directory is already in use.

Mangohero1
  • 1,832
  • 2
  • 12
  • 20