Run selenium webdriver on different browsers

Normally to run selenium webdriver we need a browser and a selenium server jar file. Selenium by default supports Mozilla Firefox browser. The question arises is how to run selenium on Internet explorer and chrome browsers.

In order to run selenium webdriver on Internet explorer and Chrome browser, you need to download the respective drivers.



Click on following link to download the drivers of different browsers
http://docs.seleniumhq.org/download/

To run webdriver using firefox:
You have to write following code.
System.setProperty("webdriver.gecko.driver","Full_Path_Of_geckodriver.exe");
WebDriver driver = new FirefoxDriver();

To run webdriver using Chrome driver:

System.setProperty("webdriver.chrome.driver", "Full_Path_Of_chromedriver.exe");
WebDriver driver = new ChromeDriver();

To run webdriver using Internet Explorer driver:
System.setProperty("webdriver.ie.driver", "Full_Path_Of_IEDriverServer.exe");

Example Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class test2 {

 public static WebDriver driver = null;
 
 public static WebDriver f_browser(String browser) {
  try {
   if (browser.equalsIgnoreCase("firefox")) {
    System.setProperty("webdriver.gecko.driver", "./MyProject/driver/geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
   }else if (browser.equalsIgnoreCase("chrome")) {
    System.setProperty("webdriver.chrome.driver", "../MyProject/driver/chromedriver.exe");
    driver = new ChromeDriver();
   }

   else if (browser.equalsIgnoreCase("ie")) {
    System.setProperty("webdriver.ie.driver", "../MyProject/driver/IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver();
   }
  }catch (Exception e) {
   System.out.println(e);
   return null;
  }
  return driver;
 }
}



<-- Previous || Next -->

1 comment:

  1. You are doing a good job and sharing your knowledge to others! it was one of the good post to read and useful to improve the knowledge as updated one, keep doing the good work.

    Selenium Training in Electronic City, Bangalore my knowledge as updated one, keep blogging.

    ReplyDelete