具有 Ruby 的 Selenium webdriver:使用现有会话 ID
Selenium webdriver with Ruby : Use existing session id
我是 selenium 网络驱动程序的新手,目前使用 Ruby 编程语言。我目前有一个场景,我需要使用 selenium 网络驱动程序执行多个 ruby 脚本,但它们必须使用相同的浏览器会话。我遵循以下 link 中提供的文档:https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings
目前,我的代码如下所示。
require 'selenium-webdriver'
# ---------------------------------------------------------
url = "http://localhost:4444/wd/hub"
driver1 = Selenium::WebDriver.for :remote, url: url, desired_capabilities: :firefox
driver1.get "http://www.msn.com"
puts driver1.title
# store the session id
sessionid = driver1.session_id
# ---------------------------------------------------------
# Try using the existing session id from driver1
driver2 = Selenium::WebDriver.for :remote, url: url, desired_capabilities: :firefox
# The below statement gives error on assignment
driver2.session_id = sessionid
driver2.get "http://www.yahoo.com"
我读了这个 post Can Selenium interact with an existing browser session? 并尝试按照此处提供的步骤操作,但无法使用现有的网络驱动程序/浏览器会话。
有没有人成功地与 selenium-webdriver 一起重用现有会话以及 Ruby?请让我知道此代码片段中遗漏了什么。
我在 python 完成了这项工作。
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
executor_url = "http://localhost:4444/wd/hub"
# Create a desired capabilities object as a starting point.
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['platform'] = "WINDOWS"
capabilities['version'] = "10"
# ------------------------ STEP 1 --------------------------------------------------
# driver1 = webdriver.Firefox()
driver1 = webdriver.Remote(command_executor=executor_url, desired_capabilities=capabilities)
driver1.get('http://google.com/')
url = driver1.command_executor._url
print(driver1.command_executor._url)
print(driver1.session_id)
print(driver1.title)
# Serialize the session id in a file
session_id = driver1.session_id
# ------------------ END OF STEP 1 --------------------------------------------------
# Pass the session id from step 1 to step 2
# ------------------------ STEP 2 --------------------------------------------------
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
temp_driver = webdriver.Remote(command_executor=executor_url)
# Replace the patched function with original function
WebDriver.execute = original_execute
return temp_driver
# read the session id from the file
driver2 = attach_to_session(executor_url, session_id)
driver2.get('http://msn.com/')
print(driver2.command_executor._url)
print(driver2.session_id)
print(driver2.title)
driver2.close()
我使用以下代码补丁解决了这个问题。
class RemoteWebDriver < Selenium::WebDriver::Driver
def initialize(bridge)
@bridge = bridge
end
end
class RemoteBridge < Selenium::WebDriver::Remote::Bridge
def self.handshake(**opts)
capabilities = opts.delete(:desired_capabilities) { Capabilities.new }
@session_id = opts.delete(:session_id)
Selenium::WebDriver::Remote::W3C::Bridge.new(capabilities, @session_id, **opts)
end
end
caps = Selenium::WebDriver::Remote::Capabilities.firefox()
remote_bridge = RemoteBridge.handshake(url: <YOUR_REMOTE_WEB_DRIVER_URL>, desired_capabilities: caps, session_id: <YOUR_SESSION_ID>)
remote_web_driver = RemoteWebDriver.new(remote_bridge)
我是 selenium 网络驱动程序的新手,目前使用 Ruby 编程语言。我目前有一个场景,我需要使用 selenium 网络驱动程序执行多个 ruby 脚本,但它们必须使用相同的浏览器会话。我遵循以下 link 中提供的文档:https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings
目前,我的代码如下所示。
require 'selenium-webdriver'
# ---------------------------------------------------------
url = "http://localhost:4444/wd/hub"
driver1 = Selenium::WebDriver.for :remote, url: url, desired_capabilities: :firefox
driver1.get "http://www.msn.com"
puts driver1.title
# store the session id
sessionid = driver1.session_id
# ---------------------------------------------------------
# Try using the existing session id from driver1
driver2 = Selenium::WebDriver.for :remote, url: url, desired_capabilities: :firefox
# The below statement gives error on assignment
driver2.session_id = sessionid
driver2.get "http://www.yahoo.com"
我读了这个 post Can Selenium interact with an existing browser session? 并尝试按照此处提供的步骤操作,但无法使用现有的网络驱动程序/浏览器会话。
有没有人成功地与 selenium-webdriver 一起重用现有会话以及 Ruby?请让我知道此代码片段中遗漏了什么。
我在 python 完成了这项工作。
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
executor_url = "http://localhost:4444/wd/hub"
# Create a desired capabilities object as a starting point.
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['platform'] = "WINDOWS"
capabilities['version'] = "10"
# ------------------------ STEP 1 --------------------------------------------------
# driver1 = webdriver.Firefox()
driver1 = webdriver.Remote(command_executor=executor_url, desired_capabilities=capabilities)
driver1.get('http://google.com/')
url = driver1.command_executor._url
print(driver1.command_executor._url)
print(driver1.session_id)
print(driver1.title)
# Serialize the session id in a file
session_id = driver1.session_id
# ------------------ END OF STEP 1 --------------------------------------------------
# Pass the session id from step 1 to step 2
# ------------------------ STEP 2 --------------------------------------------------
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
temp_driver = webdriver.Remote(command_executor=executor_url)
# Replace the patched function with original function
WebDriver.execute = original_execute
return temp_driver
# read the session id from the file
driver2 = attach_to_session(executor_url, session_id)
driver2.get('http://msn.com/')
print(driver2.command_executor._url)
print(driver2.session_id)
print(driver2.title)
driver2.close()
我使用以下代码补丁解决了这个问题。
class RemoteWebDriver < Selenium::WebDriver::Driver
def initialize(bridge)
@bridge = bridge
end
end
class RemoteBridge < Selenium::WebDriver::Remote::Bridge
def self.handshake(**opts)
capabilities = opts.delete(:desired_capabilities) { Capabilities.new }
@session_id = opts.delete(:session_id)
Selenium::WebDriver::Remote::W3C::Bridge.new(capabilities, @session_id, **opts)
end
end
caps = Selenium::WebDriver::Remote::Capabilities.firefox()
remote_bridge = RemoteBridge.handshake(url: <YOUR_REMOTE_WEB_DRIVER_URL>, desired_capabilities: caps, session_id: <YOUR_SESSION_ID>)
remote_web_driver = RemoteWebDriver.new(remote_bridge)