WebDriver methods in selenium
Once driver object is created, we can perform below methods on a driver.
- get(url) : It loads new webpage in the current window.
Example :
driver.get("http://www.google.com");
more information :
- navigate() : It allows the driver to access the browsers history. This method returns "WebDriver.Navigation" interface.
Interface has the following methods:- back(): Moves back in the history.
- forward() : Moves forward in the history.
- refresh(): Refresh the current page
- to(url) : Loads a new webpage in the current browser window.
more information :
https://onecompiler.com/posts/3sw7thdj5/sample-selenium-test-scripts-for-navigating-an-application
- quit() : It closes every associated sites
Example:
driver.quit();
- close() : It closes current site,which is open by webdriver class.
Example :
driver.close();
- switchTo() : This method helps handling web pop up's, working with frames, go to active element and back to main page or frame.
Example :
driver.switchTo().alert().accept(); // accepts the popup
driver.switchTo().alert().dismiss(); // dismiss the popup
driver.switchTo().alert().sendkeys(); // Enter the value and accept the popup
- getCurrentUrl() : It gets the current url and represents a
string.
Example :
driver.getCurrentUrl();
- getPageSource() : It gets source of the page
Example :
driver.getPageSource();
- getTitle() : Its gets the title of the current page and verifies the page title
Example :
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("Today's weather");
System.out.println(driver.getTitle());// returns the current page title and prints in the console
- manage() : It returns "WebDriver.Options" interface. Interface has the below methods
- deleteAllcookies();
- getCookies()
- timeouts()
- window()
Example:
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize(); // maximizes the current page
driver.get("https://www.google.com/");
- findElements(By by) : It finds all elements within the current page using the given mechanism.
Example :
List <WebElement> inputs= driver.findElements(By.xpath("//"));
- findElements(By by) : It finds first WebElement using the given mechanism.
Example :
WebElement search = driver.findElement(By.xpath("//"));