要在 Java 中使用 Selenium 复用已打开的 Firefox 浏览器,您需要使用 RemoteWebDriver 类和 FirefoxOptions 类。

首先,请确保您已经启动了一个 Firefox 实例,执行以下步骤:

  1. 在已经打开的 Firefox 浏览器中输入'about:config',并按下回车键。
  2. 在搜索栏中输入'marionette',然后将'marionette.enabled' 设置为'true'。
  3. 关闭浏览器。

接下来,在 Java 代码中添加以下代码来复用已打开的 Firefox 浏览器:

import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class ReuseFirefoxBrowser {
    public static void main(String[] args) {
        // 创建 FirefoxOptions 对象
        FirefoxOptions options = new FirefoxOptions();
        
        // 将 marionette 设置为 true
        options.setCapability('marionette', true);

        // 创建 RemoteWebDriver 对象,并指定 FirefoxOptions
        RemoteWebDriver driver = new RemoteWebDriver(options);

        // 打开一个网页
        driver.get('https://www.example.com');

        // 在已打开的浏览器中进行其他操作

        // 关闭浏览器
        driver.quit();
    }
}

上述代码将复用已打开的 Firefox 浏览器,并在其中打开一个网页。您可以在已打开的浏览器中执行其他操作,并在最后关闭浏览器。

请注意,您需要确保已经启动了一个 Firefox 实例,并且在代码中设置了正确的 FirefoxOptions 选项。另外,您可能需要根据您的实际情况进行一些调整。

Java Selenium 复用已打开的 Firefox 浏览器

原文地址: http://www.cveoy.top/t/topic/puBz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录