0

To clarify: I am a total newcomer in stackoverflow and Python programming so the question might not be that specific. My intention is to use one python file to log in and stay as it is, the other python file will commit itself to try and error for making functions in web scraping (the two file would control the same session). I have already started a session using selenium webdriver in file1.py. After storing the executor_url and session_id as .env in the project folder. I want to use the environment variables in file2.py (env variables has already been loaded in file2) to continue the existing session.

So the corresponding function I defined:

def attach_to_session(executor_url, session_id):
    original_execute = WebDriver.execute

    def new_command_execute(self, command, params=None):
        if command == "newSession":
            return {'success': 0, 'value': None, 'sessionId': session_id}
        return original_execute(self, command, params)

    WebDriver.execute = new_command_execute
    options = webdriver.ChromeOptions()  
    driver = webdriver.Remote(command_executor=executor_url, options=options)
    driver.session_id = session_id
    WebDriver.execute = original_execute
    return driver

driver = attach_to_session(executor_url,session_id)type here

the terminal returns

Traceback (most recent call last):
  File "d:\web_login\main_function.py", line 35, in <module>
    driver = attach_to_session(exe_url,ses_id)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\web_login\main_function.py", line 30, in attach_to_session
    driver = webdriver.Remote(command_executor=executor_url, options=options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\XX\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 206, in __init__  
    self.start_session(capabilities)
  File "C:\Users\XX\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 291, in start_session
    self.session_id = response.get("sessionId")
                      ^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get'

I don't really understand the problem so I ask for help. Any suggestion is appreciated. (also I am not so certain about the feasibility of my intention)

0 Answers0