Selenium Basic Methods

  • Opening a browser : you should download the perticular driver and keep it into your project first. for example if you are using chrome brauser you need to use chromedriver. (given on selenium download page)

System.setProperty(“webdriver.chrome.driver”,”path-to-chrome-driver”);

WebDriver driver= new ChromeDriver();

  • Maximize : to maximize the window use

driver.manage().window().maximize();

  • get() :It will load a new web page in the current browser window.

e.g. driver.get(“http://gmail.com”)

  • getCurrentUrl(): Gets a string representing the current URL that the browser is opened.

e.g. driver.getCurrentUrl();

  • getTitle(): Gets the title of the current web page.

e.g. driver.getTitle();

  • findElements() : Find all elements within the current page using the given mechanism
  • findElement():Find the first WebElement using the given method.
  • getPageSource():Get the source of the currently loaded page.
  • To(URL) : This methods Load a new web page in the current browser window.

e.g.

 
 

System.setProperty("webdriver.chrome.driver","path-to-chrome-driver");

WebDriver driver=ChromeDriver();

driver.navigate().to("http://www.google.com"); 

 

  • Back() : To move back a single step in the web browser’s history

e.g.


System.setProperty("webdriver.chrome.driver","path-to-chrome-driver");

WebDriver driver= ChromeDriver();

String URL="yahoo.com"; driver.navigate().to(URL); driver.findElement(By.linkText("Mail")).click(); driver.navigate().back(); 

 

  • Forward() : To move a single step forward in the web browser’s history.

System.setProperty("webdriver.chrome.driver","path-to-chrome-driver");

WebDriver driver= ChromeDriver();

String URL="http://yahoo.com"; 
driver.navigate().to(URL); driver.findElement(By.linkText("Mail")).click();
 driver.navigate().back(); Thread.sleep(1000); driver.navigate().forward(); 

 

  • Refresh() : It refreshes the current web page

System.setProperty("webdriver.chrome.driver","path-to-chrome-driver")

WebDriver driver= ChromeDriver();

String URL="https:/login.yahoo.com/"; 

driver.navigate().to(URL);

driver.findElement(By.linkText("Sign up")).click(); 

Thread.sleep(1000); 

driver.findElement(By.id("usernamereg-firstName")).sendKeys("amol"); 

driver.navigate().refresh(); 
  • close() : Close the current window, if there are multiple windows, it will close the current window which is active and quits the browser if it’s the last window opened currently.

e.g. driver.close();

  • quit(): Quits this driver instance, closing every associated window which is opened.

e.g. driver.quit();