Home > Enterprise >  How to use current chrome with selenium with python
How to use current chrome with selenium with python

Time:12-25

I want to use my local chrome with selenium, but every time I just get a new chrome without any cookies

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

option = webdriver.ChromeOptions()
option.add_argument(r'--user-data-dir=/Users/mac/Library/Application Support/Google/Chrome/Default') 
driver = webdriver.Chrome(chrome_options=option, executable_path="/usr/local/bin/chromedriver")
driver.get("https://twitter.com/")
  • my chrome user data dir

enter image description here

I want to use default chrome with selenium, then how to setup chrome options?

Please help, thanks!

CodePudding user response:

You have to mention the Chrome profile directory like below:

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

option = webdriver.ChromeOptions()

option.add_argument(r"user-data-dir=/Users/mac/Library/Application Support/Google/Chrome/")
option.add_argument("--profile-directory=Default")

driver = webdriver.Chrome(chrome_options=option, executable_path="/usr/local/bin/chromedriver")
driver.get("https://twitter.com/")

Before executing the code, close all the Chrome browser windows and chromedriver.exe then try.

CodePudding user response:

First, install this chrome package

pip install undetected-chromedriver

then, you can load profiles, plugins, and customize settings very easily.

import undetected_chromedriver as uc

if __name__ == '__main__':
    options = uc.ChromeOptions()
    options.add_argument(f'--user-data-dir=c:\\temp\\chrome-profile') # chrome profile location
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('--no-first-run --no-service-autorun --password-store=basic')
    options.add_argument('--load-extension=C:\\temp\\plugin-folder')
    driver = uc.Chrome(options=options)
    driver.get('https://www.google.com')
    driver.quit()
  • Related