Skip to content

Instantly share code, notes, and snippets.

@dnedbaylo
Created March 3, 2011 09:39
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dnedbaylo/852560 to your computer and use it in GitHub Desktop.
Save dnedbaylo/852560 to your computer and use it in GitHub Desktop.
PersistentWebdriver
from selenium import webdriver
from selenium.webdriver.remote.remote_connection import RemoteConnection
from selenium.webdriver.remote.errorhandler import ErrorHandler
from selenium.webdriver.remote.command import Command
class PersistentWebdriver (webdriver.Remote):
def __init__(self, session_id=None, browser_name=''):
command_executor='http://localhost:4444/wd/hub'
platform = version = ''
javascript_enabled = True
self.command_executor = command_executor
if type(self.command_executor) is str:
self.command_executor = RemoteConnection(command_executor)
self.command_executor._commands['GET_SESSION'] = ('GET', '/session/$sessionId')
self.session_id = session_id
self.capabilities = {}
self.error_handler = ErrorHandler()
if session_id:
self.connect_to_session(
browser_name=browser_name,
platform=platform,
version=version,
javascript_enabled=javascript_enabled
)
else:
self.start_session(
browser_name=browser_name,
platform=platform,
version=version,
javascript_enabled=javascript_enabled
)
def connect_to_session(self, browser_name, platform, version, javascript_enabled):
response = self.execute('GET_SESSION', {
'desiredCapabilities': {
'browserName': browser_name,
'platform': platform or 'ANY',
'version': version or '',
'javascriptEnabled': javascript_enabled
},
'sessionId': self.session_id
})
self.session_id = response['sessionId']
self.capabilities = response['value']
@ozonecatalyst
Copy link

Hey I am using webDriver in .net platform. Could you please suggest me the implementation of PersistentWebdriver Class which would run in .net ?

@dnedbaylo
Copy link
Author

sorry, not really, i am not .net developer.

@tonnydourado
Copy link

Could you please provide an example on how to instantiate this class? Does it still works, on current versions (2 years is quite a time)? I would be incredible grateful if you could. Thanks.

@senthilvasumalai
Copy link

could you please provide an example

@rajsaggi
Copy link

I am quite new to Selenium and Java Programming, can you please provide a example how to instantiate this class in selenium

@axelPalmerin
Copy link

axelPalmerin commented Mar 23, 2017

Hello, based on you class I made some changes directly on webriver.py and finally achieved reuse the session on browser.
/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py
axelPalmerin/personal@fabddb3

Ej.

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

@Debanjan-B
Copy link

Amazing !!!

@chalithaw
Copy link

hey is this really working ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment