I have a post request in Django views from which I am calling a Selenium WebDriver. I am running it on Linux.
I have set the DISPLAY in the OS environment.
Now when I call the request it does open the Selenium browser normally, but when the driver is still open and I call the request again the Selenium browser can't create the session.
DJANGO uses wsgi
application.
Switching to asgi
may not work.
I was expecting to have two drivers open but it fails on the second one.
Threading is not an issue here
class Test(generics.CreateAPIView):
def create(self, request, *args, **kwargs):
searchDict = dict(request.data)
profile = searchDict['profile']
threading.Thread(target=self.runDriver, args = (profile,)).start()
return Response(status=status.HTTP_200_OK)
def runDriver(self, profile):
br = Driver()
useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0'
profile_name = profile
username = 'samsan'
password = '##1234sAMIUL.'
br.createUCDriver(useragent, profile_name)
dr = br.ucDriver
dr.get('https://www.google.com/')
time.sleep(10)
dr.get(dr.current_url)
This is the Driver class:
class Driver:
def __init__(self):
self.driver = None
self.ucDriver = None
self.waitTime = 10
# self.logger = Logger()
# self.executable = input("Please enter chrome executable path: ")
self.executable = "seleniumDriver/chromedriver-mac-arm64/chromedriver"
def createUCDriver(self, user_agent=None, profile_name='Profile 2'):
# os.environ['DISPLAY'] = ':10.0'
options = webdriver.ChromeOptions()
if user_agent:
options.add_argument(f'--user-agent={user_agent}')
print('sys', sys.platform)
if sys.platform == 'darwin':
options.add_argument('--user-data-dir=/Users/yourusername/Library/Application Support/Google/Chrome')
elif sys.platform == 'linux' or sys.platform == 'linux2':
# options.add_argument('--user-data-dir=/home/samiul/.config/google-chrome')
options.add_argument(f'--user-data-dir={Path(__file__).parent.parent.parent / "application" / "local" / "nfs"}')
elif sys.platform == 'win32' or sys.platform == 'win64':
options.add_argument('--user-data-dir=C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data')
options.add_argument(f'--profile-directory={profile_name}')
options.add_argument('--hide-crash-restore-bubble')
options.add_argument("--disable-popup-blocking")
options.add_argument('--disable-features=InfiniteSessionRestore')
options.add_argument('--disable-features=PrivacySandboxSettings4')
if sys.platform == 'darwin':
print('mac')
driver = uc.Chrome(options=options, use_subprocess=True)
elif sys.platform == 'linux' or sys.platform == 'linux2':
service = Service(executable_path=str(Path(__file__).parent/ 'chromeDriver'/ 'chromedriver-linux64' / 'chromedriver'))
print('linux')
# driver = uc.Chrome(options=options, use_subprocess=True, driver_executable_path=Path(__file__).parent/ 'chromeDriver'/ 'chromedriver-linux64' / 'chromedriver')
driver = uc.Chrome(options=options, use_subprocess=True, service = service)
elif sys.platform == 'win32' or sys.platform == 'win64':
print('windows')
try:
driver = uc.Chrome(options=options, use_subprocess=True, driver_executable_path=str(Path(__file__).parent/ 'chromeDriver'/ 'chromedriver-win32' / 'chromedriver.exe'))
except Exception as e:
print('Error in starting Chrome', e)
self.ucDriver = driver
self.ucDriver.set_page_load_timeout(1.5 * 60)
The problem was the google chrome directory. You can only make one chrome session with that chrome directory. This should because of the file SingletonLock, this prevents me to create multiple session with the chrome directory. So I did a work around before I use the chrome directory, I simply make a temporary copy. The copied folder should be in the same folder as the original one. Then I use the chrome directory to make chrome sessions. Once I am done. I simply replace the original chrome directory with copied one but keeping the same original name. You need this:
subprocess.run(['rsync', '-aqz', '--ignore-times', '--exclude=Singleton*', source_dir, dest_dir], check=True)
This way I can make many sessions as needed. And the cookies are not lost I have kept one profile for one directory.