我無法使用JavaScript在Chrome瀏覽器中關閉選項卡 以下代碼中的最後一行js.executeScript("window.close()");
不起作用。任何人都可以幫助解決這個問題嗎?無法使用JavaScript關閉Chrome瀏覽器中的選項卡
package TestCode;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\Akash\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.gmail.com");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open('https://www.facebook.com')");
Thread.sleep(5000);
js.executeScript("window.close()");
}
}
其實這種方式不能關閉其他窗口..你需要切換該窗口首先查找所有打開的窗口爲'driver.windowHandles',然後關閉該窗口爲'driver.close()'..按照[此鏈接](https://stackoverflow.com/questions/11449179/how-can -i-近一個特定的窗口 - 使用 - 塞萊nium-webdriver-with-java).. –
這可能有助於https://stackoverflow.com/questions/18493572/closing-all-opened-tabs-except-the-first-tab-main-tab-using-webdriver – nullpointer