问题:我正在尝试关闭我的网络驱动程序,并在代码中使用了 driver.close 和 driver.quit,但没有成功。是否需要有@Before参数才能使用@After?
代码:
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Automate {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Test
public void LaunchChrome_Method1() {`
System.setProperty("webdriver.chrome.driver","C:\\Users\\HASANK\\Desktop\\tEST\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
driver.get("https://www.google.com/");
driver.findElement(By.id("lst-ib")).clear();
driver.findElement(By.id("lst-ib")).sendKeys("cnn");
driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
driver.findElement(By.linkText("CNN - Breaking News, Latest News and Videos")).click();
}
@After
public void tearDown() throws Exception {
driver.close();
driver.quit();
}
最佳答案
即使我之前也遇到过同样的问题,请尝试更新 chrome 和 chromedriver,然后再尝试一次。如果它仍然不起作用,请尝试通过创建方法来替代
public static void killDriverInstance() {
try {
String basePath = System.getProperty("user.dir");
String finalPath = basePath + "\\src\\utils\\killDriver.bat";
Process process = Runtime.getRuntime().exec(finalPath);
process.waitFor();
} catch (Exception ex) {
System.out.println("Exception in killDriverInstance method:" + ex.toString());
}
}
并在killdriver.bat文件中写入此内容
REM Kill Chrome Driver
taskkill /im chromedriver.exe /f
REM Kill Chrome browser
REM taskkill /im chrome.exe /f
关于java - Selenium WebDriver 在 Google 中不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49824196/