【问题标题】:How to execute selenium script in a lot of threads?如何在很多线程中执行 selenium 脚本?
【发布时间】:2022-01-21 18:47:32
【问题描述】:

我喜欢在很多线程中测试我的网站。但是当我尝试这样做时,我发现了一个问题。我喜欢的所有动作都发生在最后打开的窗口中。所以,第一个窗口只是卡在后台。

public class Core extends Thread{

    private static FirefoxDriver firefoxDriver;

    public Core(){
        firefoxDriver = new FirefoxDriver();
    }

    @Override
    public void run() {
        firefoxDriver.get("https://google.com/");
        firefoxDriver.close();
    }
}

public class Main {
  
    public static void main(String[] args) throws AWTException {
        
        System.setProperty("webdriver.gecko.driver", "/home/riki/Downloads/geckodriver-v0.30.0-linux64/geckodriver");

        Core core = new Core();
        Core core2 = new Core();

        core.start();    // This thread is stuck in back
        core2.start();   // This thread goes to google.com twice
    }
}
   

我真的不明白为什么会这样。你可以看到它here。在代码运行之后,第一个窗口一直挂在后面。它不关闭。当执行代码后第二个线程关闭时

【问题讨论】:

    标签: java selenium selenium-webdriver selenium-firefoxdriver java-threads


    【解决方案1】:

    这是因为您为 Forefox 驱动程序使用了静态字段。

    静态是指所有实例中的一个。所以在这里删除static

    private FirefoxDriver firefoxDriver;
    

    之后,每个线程将使用它自己的firefoxDriver 字段。

    如果要修改静态字段,应谨慎使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 2022-11-10
      • 2016-01-04
      • 1970-01-01
      • 2015-03-05
      • 2016-07-30
      • 1970-01-01
      相关资源
      最近更新 更多