Selenium可以与现有的浏览器会话进行交互吗?

有人知道Selenium(WebDriver最好)是否能够在启动Selenium客户端之前就已经运行的浏览器进行通信和操作?

我的意思是,如果Selenium能够在不使用Selenium服务器的情况下与浏览器通信(例如可以手动启动Internet Explorer)。

这是一个非常古老的function请求: 允许webdriver附加到正在运行的浏览器 。 所以现在是不可能的

这是一个重复的答案**重新连接到一个驱动程序在Pythonselenium**这适用于所有驱动程序和Java API。

  1. 打开一个驱动程序

    driver = webdriver.Firefox() #python 
  2. 从驱动程序对象提取到session_id和_url。

     url = driver.command_executor._url #"http://127.0.0.1:60622/hub" session_id = driver.session_id #'4e167f26-dc1d-4f51-a207-f761eaf73c31' 
  3. 使用这两个参数连接到您的驱动程序。

     driver = webdriver.Remote(command_executor=url,desired_capabilities={}) driver.session_id = session_id 

    而且你又连接到你的驱动程序。

     driver.get("http://www.mrsmart.in") 

有可能的。 但是你必须破解一点,有一个代码你所要做的就是运行独立的服务器和“补丁”RemoteWebDriver

 public class CustomRemoteWebDriver : RemoteWebDriver { public static bool newSession; public static string capPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", "tmp", "sessionCap"); public static string sessiodIdPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", "tmp", "sessionid"); public CustomRemoteWebDriver(Uri remoteAddress) : base(remoteAddress, new DesiredCapabilities()) { } protected override Response Execute(DriverCommand driverCommandToExecute, Dictionary<string, object> parameters) { if (driverCommandToExecute == DriverCommand.NewSession) { if (!newSession) { var capText = File.ReadAllText(capPath); var sidText = File.ReadAllText(sessiodIdPath); var cap = JsonConvert.DeserializeObject<Dictionary<string, object>>(capText); return new Response { SessionId = sidText, Value = cap }; } else { var response = base.Execute(driverCommandToExecute, parameters); var dictionary = (Dictionary<string, object>) response.Value; File.WriteAllText(capPath, JsonConvert.SerializeObject(dictionary)); File.WriteAllText(sessiodIdPath, response.SessionId); return response; } } else { var response = base.Execute(driverCommandToExecute, parameters); return response; } } } 

到目前为止,所有的解决scheme都缺乏某些function。 这是我的解决scheme:

 public class AttachedWebDriver extends RemoteWebDriver { public AttachedWebDriver(URL url, String sessionId) { super(); setSessionId(sessionId); setCommandExecutor(new HttpCommandExecutor(url) { @Override public Response execute(Command command) throws IOException { if (command.getName() != "newSession") { return super.execute(command); } return super.execute(new Command(getSessionId(), "getCapabilities")); } }); startSession(new DesiredCapabilities()); } } 

Javascript解决scheme:

我已经成功地使用此function附加到现有的浏览器会话

 webdriver.WebDriver.attachToSession(executor, session_id); 

文档可以在这里find。

我使用的是Rails + Cucumber + Selenium Webdriver + PhantomJS,而且我一直使用Selenium Webdriver的猴子补丁版本,在testing运行之间保持PhantomJS浏览器的打开状态。 看到这个博客文章: http : //blog.sharetribe.com/2014/04/07/faster-cucumber-startup-keep-phantomjs-browser-open-between-tests/

另请参阅我对这篇文章的回答: 如何从ruby文件在已打开的浏览器上执行命令

使用JavaScript selenium-webdriver客户端很容易:

首先,确保你有一个WebDriver服务器运行。 例如, 下载ChromeDriver ,然后运行chromedriver --port=9515

其次,创build这样的驱动程序:

 var driver = new webdriver.Builder() .withCapabilities(webdriver.Capabilities.chrome()) .usingServer('http://localhost:9515') // <- this .build(); 

这是一个完整的例子:

var webdriver = require('selenium-webdriver');

 var driver = new webdriver.Builder() .withCapabilities(webdriver.Capabilities.chrome()) .usingServer('http://localhost:9515') .build(); driver.get('http://www.google.com'); driver.findElement(webdriver.By.name('q')).sendKeys('webdriver'); driver.findElement(webdriver.By.name('btnG')).click(); driver.getTitle().then(function(title) { console.log(title); }); driver.quit(); 

我在python中得到了一个解决scheme,我修改了我发现的基于PersistenBrowser类的webdriver类。

https://github.com/axelPalmerin/personal/commit/fabddb38a39f378aa113b0cb8d33391d5f91dca5

replacewebdriver模块/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py

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