Read/Write .xls and .xlsx with one set of code using POI - WorkbookFactory

As we discussed in earlier post Read/Write excel using Apache POI,
we have to use:
For handling .xls fileswe use HSSF (Horrible SpreadSheet Format) related POI classes.
For handling .xlsx files, we use XSSF (XML SpreadSheet Format) related POI classes.


On this post, how we can perform excel operations with one set code for both excel formats .xls and .xlsx.
For this we have a class provided from Apache POI WorkbookFactory, which auto detects appropriate kind of Workbook (HSSFWorkbook or XSSFWorkbook) depending on the excel formats .xls or .xlsx.
Let's jump to the code implementation

Internet Explorer IE11 - F12 (debugging) not working

There is a situation where, you installed IE11 freshly and try to open the debugging window by clicking F12, but shows error instead the HTML content,

here is the fix:

Download and install the below respective patch, restart and click on F12 to get the html content.

For 32bit system:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=45134

For 64bit system:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=45154

Hope this helps!

Download files in Chrome browser using selenium WebDriver

There are situations where we need to download files from browser, and save in a specified folder on hard disk.

Find below code to download files in Chrome browser:

public class DownloadXL {
       public static void main(String[] args) {
       System.setProperty("webdriver.chrome.driver","./chromedriver.exe");
       String downloadFilepath = "c:/download";

  HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
  chromePrefs.put("profile.default_content_settings.popups", 0);
  chromePrefs.put("download.default_directory", downloadFilepath);
  ChromeOptions options = new ChromeOptions();
  HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
  options.setExperimentalOption("prefs", chromePrefs);
  options.addArguments("--test-type");
  DesiredCapabilities cap = DesiredCapabilities.chrome();
  cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
  cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
  cap.setCapability(ChromeOptions.CAPABILITY, options);
  driver = new ChromeDriver(cap);  
                driver.get("http://www.seleniumhq.org/download/");
                driver.findElement(By.linkText("32 bit Windows IE")).click();
        }
} 

Execute JavaScript in selenium to perform operations on HTML elements

Now we know how to identify html elements using only selenium or execute javascript in selenium,

Now let's see how we can perform actions on different web elements like
textbox - how we can enter text
Button - click
How to get attribute of a html element...

For input buttons
//js.executeScript("return document.getElementsByName('commit')[0].click();")
//or
//element = (WebElement) js.executeScript("return document.getElementsByName('commit')[0];");
//((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
//or
//element.click();

Execute javaScript (locators) in selenium to identify HTML element

We have seen how to identify web elements using selenium locators...there are situations where selenium alone can't able to identify web elements, there we can execute javaScript commands in selenium...

JavaScript has it's own locators to identify web elements:

getElementById - Returns single element that matches the ID.

Play with scroll bars on a page in selenium and javaScript

There are some situations in which we want to scroll to a particular element or scroll up or down across the browser.

Selenium do not have it's own methods to perform this action, so we can use javascript to play on scroll lbars.

Let's see some of the ways: