【问题标题】:Selenium WebDriver Won't Close in GoogleSelenium WebDriver 不会在 Google 中关闭
【发布时间】:2018-04-13 19:51:50
【问题描述】:

问题:我正在尝试关闭我的 webdriver,并在我的代码中同时使用了 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();
}

【问题讨论】:

    标签: java google-chrome selenium-webdriver


    【解决方案1】:

    即使我之前也遇到过同样的问题,请尝试同时更新 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
    

    【讨论】:

      【解决方案2】:

      在 C# 语言中使用这种方法:

      private void CloseChromeDriverProcesses()
              {
                  var chromeDriverProcesses = Process.GetProcesses().
                      Where(pr => pr.ProcessName == "chromedriver");
      
                  if (chromeDriverProcesses.Count() == 0)
                  {
                      return;
                  }
      
                  foreach (var process in chromeDriverProcesses)
                  {
                      process.Kill();
                  }
              }
      

      我想几分钟,你可以用 Java 语言重写这个。

      【讨论】:

        猜你喜欢
        • 2018-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多