How to reuse a selenium browser session

后端 未结 1 923
暗喜
暗喜 2020-12-28 10:27

I\'m trying to access an existing selenium browser session from a separate python process. I\'m able to get this working within the same python script, but when I break the

相关标签:
1条回答
  • 2020-12-28 11:05

    Here is an example using a one file solution, though it would work in a two file solution as well.

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from multiprocessing import Process
    import time
    
    # The main process calls this function to create the driver instance.
    def createDriverInstance():
        options = Options()
        options.add_argument('--disable-infobars')
        driver = webdriver.Chrome(chrome_options=options, port=9515)
        return driver
    
    # Called by the second process only.
    def secondProcess(executor_url, session_id):
        options = Options()
        options.add_argument("--disable-infobars")
        options.add_argument("--enable-file-cookies")
        capabilities = options.to_capabilities()
        same_driver = webdriver.Remote(command_executor=executor_url, desired_capabilities=capabilities)
        same_driver.close()
        same_driver.session_id = session_id
        same_driver.get("https://www.wikipedia.org")
        time.sleep(4)
        same_driver.quit()
    
    if __name__ == '__main__':
        driver = createDriverInstance()
        driver.get("https://google.com")
        time.sleep(2)
    
        # Pass the driver session and command_executor to the second process.
        p = Process(target=secondProcess, args=(driver.command_executor._url,driver.session_id))
        p.start()
    
    0 讨论(0)
提交回复
热议问题