Equivalent option add_argument('--user-data-dir=') in Selenium 4 (Firefox or Chrome)

Viewed 58

I had one script working with Selenium 3.X, Chrome and Windows.

from selenium import webdriver
import sys
from webdriver_manager.chrome import ChromeDriverManager


element = str(sys.argv[1])
print(element)
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("user-data-dir=C:/Users/User/AppData/Local/Google/Chrome/"+element)  #Path to your chrome profile
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)


driver.get('https://www.twitch.tv/randomstream')

This syntax was perfect to me. I could open more than one session with Selenium specifying the user data folder. I use a batch file to consecutive run the same script with the folder name as an input. It worked like a charm.

The add_argument("--user-data-dir") if I'm not wrong, let me open and configure the profile of the browser in the Selenium session. Is this true? Compared to other methods in Selenium 4.X options.SetPreference(profile,profilepath) which just copies the initial config. Is there anything in Selenium 4.X. This is my attempt:

from selenium import webdriver
import sys
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

element = str(sys.argv[1])
#element = "User Data"
options = webdriver.ChromeOptions()
#options.add_argument("--headless")
options.add_argument("user-data-dir=C:/Users/User/AppData/Local/Google/Chrome/"+element) #Path to your chrome profile
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)


driver.get('https://www.twitch.tv/randomstream')

but this is not working, when in the batch file It's opening the 2nd window with the 2nd profile the 1st is closed. This behaviour didn't happened in Selenium 3.X. Is there a way to avoid this?

0 Answers
Related