复用在重启Java应用程序时的Selenium WebDriver会话

huangapple go评论101阅读模式
英文:

Reuse Selenium WebDriver Session When Restarting Java Application

问题

每次重新启动我的Java应用程序。然后创建一个ChromeDriver()类的新实例。这反过来会启动一个新的Chrome浏览器。

我希望能够从上次离开的地方继续。所以当我为特定步骤或网页构建代码时(在我的自动化序列中的众多步骤之一)。我可以编辑代码并重新启动。而无需重新开始整个过程或序列。

这是我用Java编写的基本实现,用于启动一个已准备好进行自动化的Chrome浏览器。

  1. package jack;
  2. import org.openqa.selenium.*;
  3. import org.openqa.selenium.chrome.*;
  4. public class JacksClass {
  5. WebDriver driver;
  6. public void launchBrowser() {
  7. // 设置chrome driver的文件路径
  8. System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
  9. // 创建对象
  10. driver = new ChromeDriver();
  11. // 打开浏览器到此URL
  12. driver.get("https://google.com");
  13. }
  14. public static void main(String[] args) {
  15. JacksClass obj = new JacksClass();
  16. obj.launchBrowser();
  17. }
  18. }

通过保存会话ID是否可能实现这一点?然后在下次启动时重新使用该会话ID? 这样我就可以重新连接到当前打开的浏览器了?

是否有热心的人可以编辑我的代码以实现我的需求?谢谢,我感谢这个社区。

英文:

Every time I restart my Java application. Then create a new instance of the ChromeDriver() class. This in turn starts a new Chrome browser.

I want to be able to carry on where I left off. So while I'm building code for a given step or webpage. One of many in my automation sequence. I can edit code and relaunch. Without having to start the whole thing or sequence again.

This is my basic Java implementation to start a chrome browser which is ready for automation.

  1. package jack;
  2. import org.openqa.selenium.*;
  3. import org.openqa.selenium.chrome.*;
  4. public class JacksClass {
  5. WebDriver driver;
  6. public void launchBrowser() {
  7. // Set file path of chrome driver
  8. System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
  9. // Create object
  10. driver = new ChromeDriver();
  11. // Open our browser to this URL
  12. driver.get("https://google.com");
  13. }
  14. public static void main(String[] args) {
  15. JacksClass obj = new JacksClass();
  16. obj.launchBrowser();
  17. }
  18. }

Is this possible by saving a session ID? Then reusing that session ID next time on startup? So I can just reconnect to the current browser open?

Can an amazing individual edit my code to do what I need? Thank you, I appreciate the community.

答案1

得分: 1

如果您正在使用Chrome,有可能将新会话附加到已打开的浏览器窗口。您需要执行以下步骤:

https://medium.com/@harith.sankalpa/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd

基本上包括:
1)使用参数运行Chrome:chrome --remote-debugging-port=
2)添加ChromeOptions参数debuggerAddress

  1. ChromeOptions options = new ChromeOptions();
  2. options.setExperimentalOption("debuggerAddress", "localhost:<remote-port>");
  3. ChromeDriver driver = new ChromeDriver(options);
  4. ``` 来源 - https://medium.com/@harith.sankalpa/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd)
  5. <details>
  6. <summary>英文:</summary>
  7. If you&#39;re using Chrome, there is a possibility to attach new session to an opened browser window. What you have to do is described here:
  8. https://medium.com/@harith.sankalpa/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd
  9. Basically:
  10. 1) run Chrome with params chrome --remote-debugging-port=&lt;remote-port&gt;
  11. 2) add ChromeOptions param debuggerAddress

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress","localhost:<remote-port>");
ChromeDriver driver = new ChromeDriver(options);

  1. </details>

huangapple
  • 本文由 发表于 2020年10月20日 17:18:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64442163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定