1、要重写Remote类,防止session重建,如下:
class ReuseChrome(Remote):
def __init__(self, command_executor, session_id):
self.r_session_id= session_id
Remote.__init__(self, command_executor=command_executor, desired_capabilities={})
def start_session(self, capabilities, browser_profile=None):
if not isinstance(capabilities, dict):
raise InvalidArgumentException("Capabilities must be a dictionary")
if browser_profile:
if "moz:firefoxOptions" in capabilities:
capabilities["moz:firefoxOptions"]["profile"] = browser_profile.encoded
else:
capabilities.update({'firefox_profile': browser_profile.encoded})
self.capabilities= options.Options().to_capabilities()
self.session_id= self.r_session_id
self.w3c= False
2、访问已打开页面方式:
dr= ReuseChrome(command_executor=old_curl, session_id=sessionid)
其中old_curl需在原来打开的页面上获取:
old_curl=dr.command_executor._url #一定要用这个方法,获取当前地址是行不通的!
sessionid=dr.session_id