mirror of
https://git.zavage.net/Zavage-Software/wabot.git
synced 2024-12-22 10:29:20 -07:00
working remote firefox and chromium
This commit is contained in:
parent
3ef55c0590
commit
fd2aa7c524
19
wabot/api.py
19
wabot/api.py
@ -53,7 +53,8 @@ class BrowserProxy:
|
|||||||
'firefox2',
|
'firefox2',
|
||||||
'chromium2',
|
'chromium2',
|
||||||
'remote_chromium2',
|
'remote_chromium2',
|
||||||
'phantomjs'
|
'phantomjs',
|
||||||
|
'remote_firefox'
|
||||||
), 'webdriver_type must be firefox1, firefox2, chromium2, remote_chromium2, or phantomjs'
|
), 'webdriver_type must be firefox1, firefox2, chromium2, remote_chromium2, or phantomjs'
|
||||||
|
|
||||||
|
|
||||||
@ -212,20 +213,24 @@ class BrowserProxy:
|
|||||||
driver = None
|
driver = None
|
||||||
browser_factory = CreateBrowser()
|
browser_factory = CreateBrowser()
|
||||||
if browser == 'chromium2': # Selenium 2 - Chrome
|
if browser == 'chromium2': # Selenium 2 - Chrome
|
||||||
driver = self._create_driver_chromium2()
|
driver = self._create_driver_chromium()
|
||||||
|
|
||||||
elif browser == 'remote_chromium2':
|
elif browser == 'remote_chromium':
|
||||||
driver = browser_factory._create_driver_remote_chromium2(session_name)
|
driver = browser_factory._create_driver_remote_chromium(session_name)
|
||||||
# driver = self._create_driver_remote_chromium2(session_name)
|
|
||||||
|
|
||||||
|
elif browser == 'remote_firefox': # Selenium 1 - Firefox
|
||||||
|
driver = browser_factory._create_driver_remote_firefox(session_name)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO(MG) need to redo/rename below. selenium2/ not remote, uses native browser
|
||||||
|
# webdrviers instead of javascript
|
||||||
elif browser == 'chromium1': # Selenium 1 - Chrome without working user agent switch
|
elif browser == 'chromium1': # Selenium 1 - Chrome without working user agent switch
|
||||||
driver = self._create_driver_chromium1()
|
driver = self._create_driver_chromium1()
|
||||||
|
|
||||||
elif browser == 'firefox2': # Selenium 2 - Firefox
|
elif browser == 'firefox2': # Selenium 2 - Firefox
|
||||||
driver = self._create_driver_firefox2()
|
driver = self._create_driver_firefox2()
|
||||||
|
|
||||||
elif browser == 'firefox1': # Selenium 1 - Firefox
|
|
||||||
driver = self._create_driver_firefox1()
|
|
||||||
|
|
||||||
elif browser == 'phantomjs':
|
elif browser == 'phantomjs':
|
||||||
driver = self._create_driver_phantomjs()
|
driver = self._create_driver_phantomjs()
|
||||||
|
@ -45,9 +45,10 @@ class CreateBrowser:
|
|||||||
def pickle_filename(self, value):
|
def pickle_filename(self, value):
|
||||||
self._pickle_filename = value
|
self._pickle_filename = value
|
||||||
|
|
||||||
def _create_driver_remote_chromium2(self, session_name):
|
# i think should be chromium1, ie selenium1 or seleniumRC
|
||||||
|
def _create_driver_remote_chromium(self, session_name):
|
||||||
p = self.pickle_filename
|
p = self.pickle_filename
|
||||||
final_name = '{}-{}'.format('rchromium2', session_name)
|
final_name = '{}-{}'.format('remote-chromium', session_name)
|
||||||
driver = None
|
driver = None
|
||||||
|
|
||||||
# Definitely no browser instance already, we must instantiate
|
# Definitely no browser instance already, we must instantiate
|
||||||
@ -81,7 +82,7 @@ class CreateBrowser:
|
|||||||
|
|
||||||
# At this point, need to instantiate a new browser instance
|
# At this point, need to instantiate a new browser instance
|
||||||
sel_host = REMOTE_EXECUTOR % (EXECUTOR_PORT)
|
sel_host = REMOTE_EXECUTOR % (EXECUTOR_PORT)
|
||||||
LOGGER.info('instantianting new browser instance (chromium2)')
|
LOGGER.info('instantianting new browser instance (remote_chromium)')
|
||||||
LOGGER.info('remote selenium: %s', sel_host)
|
LOGGER.info('remote selenium: %s', sel_host)
|
||||||
|
|
||||||
opt = selenium.webdriver.chrome.options.Options()
|
opt = selenium.webdriver.chrome.options.Options()
|
||||||
@ -102,6 +103,65 @@ class CreateBrowser:
|
|||||||
return driver
|
return driver
|
||||||
|
|
||||||
|
|
||||||
|
# Pickle impl. is duped here
|
||||||
|
|
||||||
|
def _create_driver_remote_firefox(self, session_name):
|
||||||
|
p = self.pickle_filename
|
||||||
|
final_name = '{}-{}'.format('remote-firefox', session_name)
|
||||||
|
driver = None
|
||||||
|
|
||||||
|
# Definitely no browser instance already, we must instantiate
|
||||||
|
if not os.path.exists(p):
|
||||||
|
LOGGER.debug('no pickled file for saved browser instances (nothing saved yet)')
|
||||||
|
|
||||||
|
# There MAY be an open browser or an invalidated reference to a once-open browser
|
||||||
|
if os.path.exists(p):
|
||||||
|
LOGGER.debug('found pickled file for saved browser instances: %s', p)
|
||||||
|
# First, see if existing session_name browser instance exists
|
||||||
|
fp = None
|
||||||
|
drivers = {}
|
||||||
|
try:
|
||||||
|
fp = open(p, 'rb')
|
||||||
|
drivers = pickle.load(fp)
|
||||||
|
if not drivers:
|
||||||
|
raise Exception
|
||||||
|
LOGGER.debug('found saved browser instances: %s', list(drivers.keys()))
|
||||||
|
driver = drivers.get(final_name)
|
||||||
|
if not driver:
|
||||||
|
raise Exception
|
||||||
|
LOGGER.debug('connected to pickled webdriver instance: %s', final_name)
|
||||||
|
url = driver.current_url # throw error if driver isn't reliable anymore
|
||||||
|
LOGGER.info('webdriver instance is ready')
|
||||||
|
# self.driver = driver
|
||||||
|
return driver
|
||||||
|
except (FileNotFoundError, IOError) as ex:
|
||||||
|
LOGGER.error('unable to connect to existing webdriver: no pickled drivers found')
|
||||||
|
except Exception as ex:
|
||||||
|
self.driver = None
|
||||||
|
|
||||||
|
# At this point, need to instantiate a new browser instance
|
||||||
|
sel_host = REMOTE_EXECUTOR % (EXECUTOR_PORT)
|
||||||
|
LOGGER.info('instantianting new browser instance (remote_firefox)')
|
||||||
|
LOGGER.info('remote selenium: %s', sel_host)
|
||||||
|
|
||||||
|
profile = selenium.webdriver.FirefoxProfile()
|
||||||
|
profile.set_preference("general.useragent.override", USER_AGENT)
|
||||||
|
driver = selenium.webdriver.Remote(
|
||||||
|
# SELENIUM1_SERVER_PATH,
|
||||||
|
sel_host,
|
||||||
|
selenium.webdriver.DesiredCapabilities.FIREFOX.copy(),
|
||||||
|
browser_profile = profile
|
||||||
|
)
|
||||||
|
|
||||||
|
# Save to pickle
|
||||||
|
fp = open(p, 'wb')
|
||||||
|
drivers[final_name] = driver
|
||||||
|
LOGGER.info('saving browser instance to pickle: %s', final_name)
|
||||||
|
pickle.dump(drivers, fp)
|
||||||
|
|
||||||
|
return driver
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _create_driver_chromium2(self):
|
def _create_driver_chromium2(self):
|
||||||
opt = selenium.webdriver.chrome.options.Options()
|
opt = selenium.webdriver.chrome.options.Options()
|
||||||
@ -150,16 +210,7 @@ class CreateBrowser:
|
|||||||
driver = selenium.webdriver.Firefox(profile)
|
driver = selenium.webdriver.Firefox(profile)
|
||||||
return driver
|
return driver
|
||||||
|
|
||||||
def _create_driver_firefox1(self):
|
|
||||||
profile = selenium.webdriver.FirefoxProfile()
|
|
||||||
profile.set_preference("general.useragent.override", USER_AGENT)
|
|
||||||
driver = selenium.webdriver.Remote(
|
|
||||||
# SELENIUM1_SERVER_PATH,
|
|
||||||
REMOTE_EXECUTOR % (EXECUTOR_PORT),
|
|
||||||
selenium.webdriver.DesiredCapabilities.FIREFOX.copy(),
|
|
||||||
browser_profile = profile
|
|
||||||
)
|
|
||||||
return driver
|
|
||||||
|
|
||||||
def _create_driver_phantomjs(self):
|
def _create_driver_phantomjs(self):
|
||||||
# Note(MG): Selenium support for PhantomJS has been deprecated, please use headless
|
# Note(MG): Selenium support for PhantomJS has been deprecated, please use headless
|
||||||
|
Loading…
Reference in New Issue
Block a user