Python Webdriver reuse already open browser instances Python Webdriver re-use browser instance is already open

Python Webdriver reuse already open browser instances

 

Because Webdriver each instantiation will open a new new browser session, open session is not closed before the need to reuse in some cases. When such as reptiles, hoping to end the script, so that the browser is idle. When the script is re-run, it will continue to use this session to work. In doing also is automated testing, in front made a big push operation, but due to procedural error, no longer continue to operate in front of the complex restart.

Personally I feel that this is very useful, but there's no official API to provide this functionality, hard Soso, on the Internet for two java version of the http://blog.csdn.net/wwwqjpcom/article/details/51232302 and  http://woxiangbo.iteye.com/blog/2372683
looked under the java and python source code in fact the driving principle of the process is very similar.

Open a Chrome session:

from selenium import webdriver
driver = webdriver.Chrome()

Running the above script, it will start the browser and exit. Because there is no call quit()method, so the browser session still exist. However, the code creates driveran object has gone, in theory, can not use a script to control this browser. It will turn into a zombie browser, only manually kill it.

Start a browser session by webdriver probably have three stages:

  • 1, start the browser driving agent (driver hromedriver, Firefox, and so on);
  • 2. Create a command executor. Agents used to send commands to operate;
  • 3, use a proxy to create a new browser session, the agent will communicate with the browser. Use sessionIdto identify the session.

So long as the get in Phase 2 and Phase actuator 3 sessionIDwill be able to restore the previous session. Both api can get there directly:

from selenium import webdriver

driver = webdriver.Chrome()
executor_url = driver.command_executor._url
session_id = driver.session_id
print(session_id)
print(executor_url)
driver.get("http://www.spiderpy.cn/")

Obtained like this output (the first session sessionId, the second actuator is connected to the command):

397d725f042a076f7d4a82f7d3fead13
http://127.0.0.1:52869

Everything is ready, following began to realize the function of the session before reuse, the Stack Overflow just mentioned, to achieve this:

from selenium import webdriver

driver = webdriver.Chrome()
executor_url = driver.command_executor._url
session_id = driver.session_id
driver.get("http://www.spiderpy.cn/")

print(session_id)
print(executor_url)

driver2 = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
driver2.session_id = session_id
print(driver2.current_url)

The reason may be because the version of it, anyway, I run in the environment, the effect is achieved, to reconnect to the session, but it opens a new blank session. I looked under the Remotecategory of source code and found that because each instantiation will call start_sessionthis method to create a new session. So the solution is to inherit and override this class. A custom ReuseChromeclass override the start_sessionmethod so that it is no longer a new session, passed in session_id:

from selenium.webdriver import Remote
from selenium.webdriver.chrome import options
from selenium.common.exceptions import InvalidArgumentException 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): """ 重写start_session方法 """ 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

The second connector is then rewritten to use ReuseChromeclasses:

from selenium import webdriver

#  第一次使用Chrome() 新建浏览器会话
driver = webdriver.Chrome()

# 记录 executor_url 和 session_id 以便复用session
executor_url = driver.command_executor._url
session_id = driver.session_id
# 访问百度 driver.get("http://www.spiderpy.cn/") print(session_id) print(executor_url) # 假如driver对象不存在,但浏览器未关闭 del driver # 使用ReuseChrome()复用上次的session driver2 = ReuseChrome(command_executor=executor_url, session_id=session_id) # 打印current_url为百度的地址,说明复用成功 print(driver2.current_url) driver2.get("https://www.baidu.com")

So we can successfully connect to the last did not close the browser session.

Because Webdriver each instantiation will open a new new browser session, open session is not closed before the need to reuse in some cases. When such as reptiles, hoping to end the script, so that the browser is idle. When the script is re-run, it will continue to use this session to work. In doing also is automated testing, in front made a big push operation, but due to procedural error, no longer continue to operate in front of the complex restart.

Personally I feel that this is very useful, but there's no official API to provide this functionality, hard Soso, on the Internet for two java version of the http://blog.csdn.net/wwwqjpcom/article/details/51232302 and  http://woxiangbo.iteye.com/blog/2372683
looked under the java and python source code in fact the driving principle of the process is very similar.

Open a Chrome session:

from selenium import webdriver
driver = webdriver.Chrome()

Running the above script, it will start the browser and exit. Because there is no call quit()method, so the browser session still exist. However, the code creates driveran object has gone, in theory, can not use a script to control this browser. It will turn into a zombie browser, only manually kill it.

Start a browser session by webdriver probably have three stages:

  • 1, start the browser driving agent (driver hromedriver, Firefox, and so on);
  • 2. Create a command executor. Agents used to send commands to operate;
  • 3, use a proxy to create a new browser session, the agent will communicate with the browser. Use sessionIdto identify the session.

So long as the get in Phase 2 and Phase actuator 3 sessionIDwill be able to restore the previous session. Both api can get there directly:

from selenium import webdriver

driver = webdriver.Chrome()
executor_url = driver.command_executor._url
session_id = driver.session_id
print(session_id)
print(executor_url)
driver.get("http://www.spiderpy.cn/")

Obtained like this output (the first session sessionId, the second actuator is connected to the command):

397d725f042a076f7d4a82f7d3fead13
http://127.0.0.1:52869

Everything is ready, following began to realize the function of the session before reuse, the Stack Overflow just mentioned, to achieve this:

from selenium import webdriver

driver = webdriver.Chrome()
executor_url = driver.command_executor._url
session_id = driver.session_id
driver.get("http://www.spiderpy.cn/")

print(session_id)
print(executor_url)

driver2 = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
driver2.session_id = session_id
print(driver2.current_url)

The reason may be because the version of it, anyway, I run in the environment, the effect is achieved, to reconnect to the session, but it opens a new blank session. I looked under the Remotecategory of source code and found that because each instantiation will call start_sessionthis method to create a new session. So the solution is to inherit and override this class. A custom ReuseChromeclass override the start_sessionmethod so that it is no longer a new session, passed in session_id:

from selenium.webdriver import Remote
from selenium.webdriver.chrome import options
from selenium.common.exceptions import InvalidArgumentException 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): """ 重写start_session方法 """ 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

The second connector is then rewritten to use ReuseChromeclasses:

from selenium import webdriver

#  第一次使用Chrome() 新建浏览器会话
driver = webdriver.Chrome()

# 记录 executor_url 和 session_id 以便复用session
executor_url = driver.command_executor._url
session_id = driver.session_id
# 访问百度 driver.get("http://www.spiderpy.cn/") print(session_id) print(executor_url) # 假如driver对象不存在,但浏览器未关闭 del driver # 使用ReuseChrome()复用上次的session driver2 = ReuseChrome(command_executor=executor_url, session_id=session_id) # 打印current_url为百度的地址,说明复用成功 print(driver2.current_url) driver2.get("https://www.baidu.com")

So we can successfully connect to the last did not close the browser session.

Guess you like

Origin www.cnblogs.com/xaiobong/p/11564171.html