Can Selenium interact with an existing browser session?

后端 未结 11 886
野性不改
野性不改 2020-11-22 08:47

Does anybody know if Selenium (WebDriver preferably) is able to communicate with and act through a browser that is already running before launching a Selenium Client?

相关标签:
11条回答
  • 2020-11-22 09:12

    This is a pretty old feature request: Allow webdriver to attach to a running browser . So it's officially not supported.

    However, there is some working code which claims to support this: https://web.archive.org/web/20171214043703/http://tarunlalwani.com/post/reusing-existing-browser-session-selenium-java/.

    0 讨论(0)
  • 2020-11-22 09:14

    I got a solution in python, I modified the webdriver class bassed on PersistenBrowser class that I found.

    https://github.com/axelPalmerin/personal/commit/fabddb38a39f378aa113b0cb8d33391d5f91dca5

    replace the webdriver module /usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py

    Ej. to use:

    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    runDriver = sys.argv[1]
    sessionId = sys.argv[2]
    
    def setBrowser():
        if eval(runDriver):
            webdriver = w.Remote(command_executor='http://localhost:4444/wd/hub',
                         desired_capabilities=DesiredCapabilities.CHROME,
                         )
        else:
            webdriver = w.Remote(command_executor='http://localhost:4444/wd/hub',
                                 desired_capabilities=DesiredCapabilities.CHROME,
                                 session_id=sessionId)
    
        url = webdriver.command_executor._url
        session_id = webdriver.session_id
        print url
        print session_id
        return webdriver
    
    0 讨论(0)
  • 2020-11-22 09:20

    This snippet successfully allows to reuse existing browser instance yet avoiding raising the duplicate browser. Found at Tarun Lalwani's blog.

    from selenium import webdriver
    from selenium.webdriver.remote.webdriver import WebDriver
    
    # executor_url = driver.command_executor._url
    # session_id = driver.session_id
    
    def attach_to_session(executor_url, session_id):
        original_execute = WebDriver.execute
        def new_command_execute(self, command, params=None):
            if command == "newSession":
                # Mock the response
                return {'success': 0, 'value': None, 'sessionId': session_id}
            else:
                return original_execute(self, command, params)
        # Patch the function before creating the driver object
        WebDriver.execute = new_command_execute
        driver = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
        driver.session_id = session_id
        # Replace the patched function with original function
        WebDriver.execute = original_execute
        return driver
    
    bro = attach_to_session('http://127.0.0.1:64092', '8de24f3bfbec01ba0d82a7946df1d1c3')
    bro.get('http://ya.ru/')
    
    0 讨论(0)
  • 2020-11-22 09:22

    All the solutions so far were lacking of certain functionality. Here is my solution:

    public class AttachedWebDriver extends RemoteWebDriver {
    
        public AttachedWebDriver(URL url, String sessionId) {
            super();
            setSessionId(sessionId);
            setCommandExecutor(new HttpCommandExecutor(url) {
                @Override
                public Response execute(Command command) throws IOException {
                    if (command.getName() != "newSession") {
                        return super.execute(command);
                    }
                    return super.execute(new Command(getSessionId(), "getCapabilities"));
                }
            });
            startSession(new DesiredCapabilities());
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:25

    Javascript solution:

    I have successfully attached to existing browser session using this function

    webdriver.WebDriver.attachToSession(executor, session_id);
    

    Documentation can be found here.

    0 讨论(0)
提交回复
热议问题