0
  • I have a selenium browser where I've added options to use my google chrome profile when the browser is opened.

  • I know there will be an error when trying to create my selenium browser if chrome is opened elsewhere with the same profile.

  • But despite there being an error the browser still opens

What I want to do is to still be able to interact with this browser, since it still opens with the profile I wanted it to, (and for various reasons I don't want to close my other chrome instances)

I had to through in try and except so the program doesn't stop, but I think the browser gets garbage collected in the try block.

So is there a way to stop this getting garbage collected or can I find all browsers opened by webdriver ?and then set one of them to a new browser

Here's the code:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
try:
    chrome_options.add_argument("user-data-dir=C:\\Users\\coderoftheday\\AppData\\Local\\Google\\Chrome\\User Data\\")
    browser = webdriver.Chrome("chromedriver.exe", options=chrome_options)

except:
    pass

browser.get('https://www.google.co.uk/')

Error:

NameError: name 'browser' is not defined
coderoftheday
  • 1,987
  • 4
  • 7
  • 21

1 Answers1

0

Isn't this just a python variable scope issue? See https://stackoverflow.com/a/25666911/1387701

Simplest solution:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
browser = None
try:
    chrome_options.add_argument("user-data-dir=C:\\Users\\coderoftheday\\AppData\\Local\\Google\\Chrome\\User Data\\")
    browser = webdriver.Chrome("chromedriver.exe", options=chrome_options)

except:
    #code here to kill existing chrome instance
    #retry opening chrome:
    browser = webdriver.Chrome("chromedriver.exe", options=chrome_options)

browser.get('https://www.google.co.uk/')
DMart
  • 2,401
  • 1
  • 14
  • 19
  • no since browser will still equate to None after the try and accept, I want the browser to equal the web driver, since despite an error the web driver still opens – coderoftheday Oct 27 '20 at 17:13
  • yes i tried this initially, the error was ```AttributeError: 'NoneType' object has no attribute 'get'``` – coderoftheday Oct 27 '20 at 17:26
  • its something to with the profile being used already, but I already know how to deal with that error, I just need more than one profile open for a specific reason. – coderoftheday Oct 27 '20 at 17:44
  • So it should be trivial to catch and kill the existing chrome that is open. Would be helpful to see exact error message you get from chrome. This might be a limition of chrome with regard to user profiles. – DMart Oct 27 '20 at 18:17
  • this is the error ```selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir ``` – coderoftheday Oct 27 '20 at 18:53
  • you might want to look at the answer here about chrome profiles: https://stackoverflow.com/a/60048564/1387701 – DMart Oct 27 '20 at 19:19