所以我正在努力学习如何使用Selenium,我在互联网上找到了一个关于如何做到这一点的教程(http://toolsqa.com/selenium-webdriver/first-test-case/)。我知道我使用的是Internet Explorer而不是firefox,但我已按照如何设置IED驱动程序的说明进行操作。问题是,当我使用他们的代码只是打开和关闭一个窗口时,它会打开它,并忽略那里的driver.quit()。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class FirstTestCase {
public static void main(String[] args) {
String service = "C:\\Users\\abc\\Documents\\IE Explorer Server\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", service);
InternetExplorerDriver driver = new InternetExplorerDriver();
//Launch the Online Store Website
driver.get("http://www.store.demoqa.com");
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
//Wait for 5 Sec
Thread.sleep(5000);
// Close the driver
driver.quit();
System.out.println(",");
}
}
它打印我关闭后设置打印的逗号,但它实际上并没有关闭浏览器。 Thread.sleep()也会出错。我已经找了3个多小时才得到一些解决方案,但找不到任何东西。
答案 0 :(得分:0)
试试这个:
public static void main(String[] args) throws InterruptedException {
String service = "C:\\Users\\abc\\Documents\\IE Explorer Server\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", service);
InternetExplorerDriver driver = new InternetExplorerDriver();
//Launch the Online Store Website
driver.get("http://www.store.demoqa.com");
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
//Wait for 5 Sec
Thread.sleep(5000);
// Close the driver
driver.quit();
System.out.println(",");
}
答案 1 :(得分:0)
以下是您的问题的答案:
我的代码块中没有发现任何重大错误。关于解决方案的一些建议:
我使用Selenium 3.4.0,IEDriverServer 3.4.0&amp ;;重新测试了您的代码块。 MSIE 10.0只有一个附加参数
public static void main(String[] args) throws InterruptedException
你的代码执行得很好。每当您在代码中添加Thread.sleep(5000);
时,您可以考虑添加throws InterruptedException
。但同样,根据最佳做法,我们必须避免使用Thread.sleep()
并将其替换为ImplicitlyWait
或ExplicitWait
。
InternetExplorerDriver
实现来启动驱动程序。根据W3C标准,您必须考虑使用WebDriver
接口。driver.quit()
不仅会关闭驱动程序,还会杀死驱动程序实例并释放内存。以下是您自己的代码,其中包含一些小调整,适用于我这方面:
public static void main(String[] args) throws InterruptedException
{
String service = "C:\\your_directory\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", service);
WebDriver driver = new InternetExplorerDriver();
//Launch the Online Store Website
driver.get("http://www.store.demoqa.com");
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
// Quit the driver
driver.quit();
System.out.println(",");
}
请告诉我这是否是您的问题