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

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

Reuse Selenium WebDriver Session When Restarting Java Application

问题

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

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

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

package jack;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;

public class JacksClass {

       WebDriver driver;

       public void launchBrowser() {

              // 设置chrome driver的文件路径
              System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

              // 创建对象
              driver = new ChromeDriver();

              // 打开浏览器到此URL
              driver.get("https://google.com");

       }

       public static void main(String[] args) {

              JacksClass obj = new JacksClass();

              obj.launchBrowser();

       }
}

通过保存会话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.

    package jack;
    import org.openqa.selenium.*;
    import org.openqa.selenium.chrome.*;
    
    public class JacksClass {
    
           WebDriver driver;
           
           public void launchBrowser() {
                  
                  // Set file path of chrome driver
                  System.setProperty("webdriver.chrome.driver",  "C:\\chromedriver.exe");
                  
                  // Create object
                  driver = new ChromeDriver();
                  
                  // Open our browser to this URL
                  driver.get("https://google.com");
                  
           }
           
           public static void main(String[] args) {
    
                  JacksClass obj = new JacksClass();
                  
                  obj.launchBrowser();
           
           }
    }

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

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "localhost:<remote-port>");
ChromeDriver driver = new ChromeDriver(options);
``` 来源 - https://medium.com/@harith.sankalpa/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd)

<details>
<summary>英文:</summary>

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:

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

Basically:
1) run Chrome with params chrome --remote-debugging-port=&lt;remote-port&gt;
2) add ChromeOptions param debuggerAddress

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


</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:

确定