Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

driver.close() method is not working in Selenium WebDriver on Firefox

I am writing a simple program in Eclipse using JUnit annotation.

diver.close() is not closing my Firefox browser after the tests. It works fine with Chrome. Code snippet is here.

public class FireFox1 {
    WebDriver driver;

    @Before
    public void setUp() {
        driver= new FirefoxDriver();
        driver.get("http://book.theautomatedtester.co.uk/chapter4");
    }

    @After
    public void tearDown() {
        driver.close();
    }

    @Test
    public void testExamples() {
        WebElement element= driver.findElement(By.id("nextBid"));
        element.sendKeys("100");     

    }
} 
like image 205
Sanjay Karkera Avatar asked Jun 28 '15 04:06

Sanjay Karkera


People also ask

How do I close Firefox browser in Selenium Webdriver?

WebDriver webdriver; ProfilesIni profile = new ProfilesIni(); FirefoxProfile myprofile = profile. getProfile("myProfileName"); webdriver = new FirefoxDriver(myprofile); Now webdriver. quit(); will close the firefox browser after the test has run.

How do I run Selenium tests in Firefox using Firefox drivers?

Step 1: Navigate to the official Selenium website. Under third-party drivers, one will find all the drivers. Just click on the Mozilla GeckoDriver documentation as shown below. Now, it will navigate to the GeckoDriver downloads link, where one can download the suitable driver based on the OS as it is platform agnostic.

What is the correct answer regarding driver close () driver quit ()?

driver. quit() : The quit() method quits the driver, closing every associated window. driver. close() : The close() method closes the currently focused window, quitting the driver if the current window is the only open window.

How do I close a driver in Selenium?

close() The close() method is a Webdriver command that closes the browser window currently in focus. It is best to use the close() command when multiple browser tabs or windows are open. If only one window is open in the entire browser, then the close() command will quit the entire browser session.


1 Answers

Better use driver.quit() method. It closes the browser, but due to some unknown issues it throws NullPointerException. Just catch it..

try{
    driver.quit();
   }catch (Exception e){
      System.out.println("Nothing to do with it");
      }
like image 83
sras Avatar answered Oct 21 '22 03:10

sras