Configure Selenium WebDriver to use Internet Explorer

0 votes

As of 2015, the WebDriver specification became part of the W3C standard. The drivers are external, to make it more robust. We need to set the path to the driver executable in order to use the specific
driver. This section describes how to use Internet Explorer.

Download the latest iedriver from the following location: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

@Test
public void useInternetExplorerDriver() {
  // Start browser
  System.setProperty("webdriver.ie.driver", FileUtil.findFileOnPath("IEDriverServer.exe"));
  final InternetExplorerDriver driver = new InternetExplorerDriver();
  
  // Open website
  driver.get("http://www.selenium-in-action.io");
  
  // Close browser
  driver.quit();
}

Find file on path

public class FileUtil {

  public static String findFileOnPath(final String fileName) {
    if (new File(FileUtil.class.getClassLoader().getResource(fileName).getPath()).canExecute()) {
      return FileUtil.class.getClassLoader().getResource(fileName).getPath();
    } else {
      return new File("src/main/resources/" + fileName).getAbsolutePath();
    }
  }

}

Configure Selenium WebDriver to use Chrome

0 votes

As of 2015, the WebDriver specification became part of the W3C standard. The drivers are external, to make it more robust. We need to set the path to the driver executable in order to use the specific driver. (use `chmod +x [filename]` to make it executable on UNIX based systems) This section describes how to use Chrome.

Download the latest chromedriver from the following location: https://sites.google.com/a/chromium.org/chromedriver/downloads

@Test
public void useChromeDriver() {
  // Start browser
  System.setProperty("webdriver.chrome.driver", FileUtil.findFileOnPath("mac/chromedriver"));
  final ChromeDriver driver = new ChromeDriver();
  
  // Open website
  driver.get("http://www.selenium-in-action.io");
  
  // Close browser
  driver.quit();
}

Find file on path

public class FileUtil {

  public static String findFileOnPath(final String fileName) {
    if (new File(FileUtil.class.getClassLoader().getResource(fileName).getPath()).canExecute()) {
      return FileUtil.class.getClassLoader().getResource(fileName).getPath();
    } else {
      return new File("src/main/resources/" + fileName).getAbsolutePath();
    }
  }

}

Configure Selenium WebDriver to use FireFox

0 votes

As of 2015, the WebDriver specification became part of the W3C standard. The drivers are external, to make it more robust. We need to set the path to the driver executable in order to use the specific driver. (use `chmod +x [filename]` to make it executable on UNIX based systems) This section describes how to use Firefox.

Download the latest geckodriver from the following location: https://github.com/mozilla/geckodriver/releases

 

@Test
public void useFireFoxDriver() {
  // Start browser
  System.setProperty("webdriver.gecko.driver", FileUtil.findFileOnPath("mac/geckodriver"));
  final FirefoxDriver driver = new FirefoxDriver();
  
  // Open website
  driver.get("http://www.selenium-in-action.io");
  
  // Close browser
  driver.quit();
}

Find file on path

public class FileUtil {

  public static String findFileOnPath(final String fileName) {
    if (new File(FileUtil.class.getClassLoader().getResource(fileName).getPath()).canExecute()) {
      return FileUtil.class.getClassLoader().getResource(fileName).getPath();
    } else {
      return new File("src/main/resources/" + fileName).getAbsolutePath();
    }
  }

}

Locating Web Elements by walking up the DOM

1 vote

This is one of the rare cases where we need use XPath, as parent selecting is not supported in CSS. We prepared a sample to visualize this case. Take a look on the following website: when to use XPath

As you can see the amount of unique identifiers is very limited. However, we can select the `edit` or `delete` button based on the value which is present in the first table-cell.

The following example will select the `edit` button of the third row.

//td[contains(text(), '678')]/../td/button[@class = 'edit']

As you can see, we first select the right table-cell based on the text it contains. Then we go one level up ( ../) in the HTML structure and select the right button based on the `class`.

 

Examine your knowledge and play the ‘locator game‘.