0

I am trying to navigate to the active chrome tab using Selenium and Python code. However, when I run my code, it opens a new window with a new profile, not the profile that I have specified. I have tried many things to solve this issue, but I do not know what the problem is.

Here is my code:

   from selenium import webdriver
   from selenium.webdriver.chrome.options import Options
   from selenium.webdriver.common.by import By
   from selenium.webdriver.support.ui import WebDriverWait
   from selenium.webdriver.support import expected_conditions as EC
   
   chrome_options = Options()
   chrome_options.add_argument('--remote-debugging-port=9222')
   #chrome_options.add_argument('--user-data-dir=C:\\Users\\loai\\AppData\\Local\\Google\\Chrome\\User Data')
   #chrome_options.add_argument('--profile-directory=Profile 1')
   
   driver = webdriver.Chrome(executable_path="C:\\path\\to\\chromedriver.exe", options=chrome_options)
   driver.get("https://nft.bueno.art/NS1PNAih8UHV6y1XviWJU/art/kZK_iEBrl-kK8NY_UznyI/preview")
   
   # Wait for the element to be present
   wait = WebDriverWait(driver, 10)
   element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".virtuoso-grid-item")))
   
   # Now you can execute your script
   driver.execute_script("arguments[0].click();", element)

I have specified the profile path 'C:/Users/loai/AppData/Local/Google/Chrome/User Data/Profile 1', but it still creates a new profile and continues with it. I want to run my code with the already active tab that has the specified URL that I have mentioned in the browser.get line.

Does anyone have a suggestion on how to fix this issue or has had a similar issue in the past?

My chrome browser version is Version 109.0.5414.75 (Official Build) (64-bit).

I am trying to navigate to the active chrome tab using Selenium and Python code. However, when I run my code, it opens a new window with a new profile, not the profile that I have specified.

1 Answer 1

0

To open specific profile, you should set 2 chrome options:

chrome_options.add_argument('--user-data-dir=C:\\Users\\{user}\\AppData\\Local\\Google\\Chrome\\User Data')
chrome_options.add_argument('--profile-directory=Profile 1')

UPDATE

It's allowed to open the same user data directory only once, otherwise it will fail. If you need only one chrome session to work, the above options should work without issues. But if you want to open several different profiles at a time as separate sessions, avoid spaces in user data paths. Better to rename folders in AppData to be used with selenium not to have spaces and then options will look like:

chrome_options.add_argument('--user-data-dir=C:\\Users\\{user}\\AppData\\Local\\Google\\Chrome\\UserData')
chrome_options.add_argument('--profile-directory=Profile1')
Sign up to request clarification or add additional context in comments.

2 Comments

i have added your code in the above code and commented on them because when uncomment it it open the specified profile but not going to the URL and when comment them it is go the URL but not the specified profile so how we can solve this issue to go to the url with the specified profile can you debug on your end and show me the results?
well, I assume the problem is you have already Chrome sessions opened. When you use "user-data-dir" you cannot open the same directory twice. The most brainstorming is that I had others profile opened, not the one I was launching with selenium but it still failed to start new session as if profile is already opened. I fixed that by creating a copy of my "User Data" and renamed it to "UserData" (without space), as well as removed all spaces in Profile folder names, for example Profile1, Profile2 etc. So these paths without spaces worked perfectly. (updated answer, check it too)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.