我喜歡在很多執行緒中測驗我的網站。但是當我嘗試這樣做時,我發現了一個問題。我喜歡的所有動作都發生在最后打開的視窗中。所以,第一個視窗只是卡在后臺。
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
}
}
我真的不明白為什么會這樣。你可以在這里看到它。在代碼運行之后,第一個視窗一直掛在后面。它不關閉。當第二個執行緒在執行代碼后關閉時
uj5u.com熱心網友回復:
這是因為您為 Forefox 驅動程式使用了靜態欄位。
靜態是指所有實體中的一個。所以在這里洗掉static。
private FirefoxDriver firefoxDriver;
之后,每個執行緒將使用它自己的firefoxDriver欄位。
如果要修改靜態欄位,應謹慎使用它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/417974.html
標籤:
