Selenium get() method and navigate() method

Selenium get() method

get() method in selenium is one from the many different methods available in webdriver interface. Selenium get() method is used to open URL in browser.

Selenium get() and navigate() Methods

  • get(String) -> It accepts string as a parameter and returns nothing.
  • get() method is used to open URL in current loaded browser to load a new web page.
  • URL which you provide in get() method must start with http:// or https://
  • Syntax of using get() method is driver.get("http://www.yourdomainname.com")
  • get() method will wait for page load completely.

Selenium get() method example

package testPackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class getMethodExample {

	public static void main(String[] args) {
		
		//Set path of chrome driver file.
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
		//Initialize and Launch chrome browser.
		WebDriver driver=new ChromeDriver();
	    		
	    //Open URL in current loaded browser. 
		driver.get("http://www.only-testing-blog.blogspot.com/");	    
	}
}
Here, get() method will open given URL http://www.only-testing-blog.blogspot.com/ in current open browser.

Selenium navigate() method

navigate() is method of webdriver interface which returns a type Navigation which is inner interface of webdriver interface. navigate() method allows driver to access browser navigation history and navigate to given URL.
  • Navigation interface have many different methods like to(), back(), forward() and refresh().
  • navigate().to(String URL) - Navigate to provided URL.
  • navigate().refresh() - refresh the current webpage.
  • navigate().back() - Navigate back.
  • navigate().forward() - Navigate forward.
Here is example of using different methods of navigation interface with navigate() method.

Selenium navigate() method example

package testPackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class getMethodExample {

	public static void main(String[] args) {
		
		//Set path of chrome driver file.
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
		//Initialize and Launch chrome browser.
		WebDriver driver=new ChromeDriver();
	    		
	    //Open URL in current loaded browser.
		driver.get("http://www.only-testing-blog.blogspot.com/");	
		
		//Navigate to given URL.
		driver.navigate().to("http://www.only-testing-blog.blogspot.com/2014/05/form.html");
		
		//Navigate back
		driver.navigate().back();
		
		//Navigate forward.
		driver.navigate().forward();
		
		//Refresh the page.
		driver.navigate().refresh();
	}
}
  • driver.navigate().to("http://www.only-testing-blog.blogspot.com/2014/05/form.html") -> It will navigate to given url http://www.only-testing-blog.blogspot.com/2014/05/form.html.
  • driver.navigate().back(); -> It will navigate back to previous url http://www.only-testing-blog.blogspot.com/
  • driver.navigate().forward(); -> It will navigate to previous url http://www.only-testing-blog.blogspot.com/
  • driver.navigate().refresh(); -> It will refresh the page.

No comments:

Post a Comment