要在Selenium中复用FirefoxDriver,可以使用FirefoxOptions类中的addArguments方法来添加选项。下面是一个示例代码:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class ReuseFirefoxDriver {
    public static void main(String[] args) {
        // 创建FirefoxOptions对象
        FirefoxOptions options = new FirefoxOptions();

        // 添加要复用的FirefoxDriver的选项
        options.addArguments("-profile", "C:/path/to/firefox/profile");

        // 创建第一个FirefoxDriver实例
        WebDriver driver1 = new FirefoxDriver(options);

        // 创建第二个FirefoxDriver实例
        WebDriver driver2 = new FirefoxDriver(options);

        // 在两个浏览器实例中进行操作
        driver1.get("https://www.example.com");
        System.out.println(driver1.getTitle());

        driver2.get("https://www.google.com");
        System.out.println(driver2.getTitle());

        // 关闭浏览器实例
        driver1.quit();
        driver2.quit();
    }
}

在上面的示例中,我们创建了一个FirefoxOptions对象,并使用addArguments方法添加了要复用的FirefoxDriver的选项,其中"-profile", "C:/path/to/firefox/profile"表示要复用的Firefox配置文件的路径。然后,我们可以创建多个FirefoxDriver实例,并在这些实例中进行操作。最后,记得关闭每个浏览器实例

selenium复用FirefoxDriver

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

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