- Selenium Tutorial
- Selenium - Home
- Selenium - Overview
- Selenium - Components
- Selenium - Automation Testing
- Selenium - Environment Setup
- Selenium - Remote Control
- Selenium IDE Tutorial
- Selenium - IDE Introduction
- Selenium - Features
- Selenium - Limitations
- Selenium - Installation
- Selenium - Creating Tests
- Selenium - Creating Script
- Selenium - Store Variables
- Selenium - Alerts & Popups
- Selenium - Selenese Commands
- Selenium - Actions Commands
- Selenium - Accessors Commands
- Selenium - Assertions Commands
- Selenium - Assert/Verify Methods
- Selenium - Locating Strategies
- Selenium - Script Debugging
- Selenium - Verification Points
- Selenium - Pattern Matching
- Selenium - JSON Data File
- Selenium - Browser Execution
- Selenium - User Extensions
- Selenium - Code Export
- Selenium - JavaScript Functions
- Selenium - Plugins
- Selenium WebDriver Tutorial
- Selenium - Introduction
- Selenium WebDriver vs RC
- Selenium - Installation
- Selenium - First Test Script
- Selenium - Driver Sessions
- Selenium - Browser Options
- Selenium - Chrome Options
- Selenium - Edge Options
- Selenium - Firefox Options
- Selenium - Safari Options
- Selenium Web Elements
- Selenium - File Upload
- Selenium - Locator Strategies
- Selenium - Relative Locators
- Selenium - Finders
- Selenium - Find All Links
- Selenium - User Interactions
- Selenium - WebElement Commands
- Selenium - Browser Interactions
- Selenium - Browser Commands
- Selenium - Browser Navigation
- Selenium - Alerts & Popups
- Selenium - Handling Forms
- Selenium - Windows and Tabs
- Selenium - Handling Links
- Selenium - Input Boxes
- Selenium - Radio Button
- Selenium - Checkboxes
- Selenium - Dropdown Box
- Selenium - Handling IFrames
- Selenium - Handling Cookies
- Selenium - Date Time Picker
- Selenium - Dynamic Web Tables
- Selenium - Actions Class
- Selenium - Action Class
- Selenium - Keyboard Events
- Selenium - Key Up/Down
- Selenium - Copy and Paste
- Selenium - Handle Special Keys
- Selenium - Mouse Events
- Selenium - Drag and Drop
- Selenium - Scroll Operations
- Selenium - Waiting Strategies
- Selenium - Explicit/Implicit Wait
- Selenium - Support Features
- Selenium - Multi Select
- Selenium - Wait Support
- Selenium - Select Support
- Selenium - Color Support
- Selenium - Errors & Logging
- Selenium - Logging
- Selenium - Exception Handling
- Selenium - Miscellaneous
- Selenium - Handling Ajax Calls
- Selenium - JSON Data File
- Selenium - CSV Data File
- Selenium - Excel Data File
- Selenium - Cross Browser Testing
- Selenium - Multi Browser Testing
- Selenium - Multi Windows Testing
- Selenium - JavaScript Executor
- Selenium - Headless Execution
- Selenium - Capture Screenshots
- Selenium - Capture Videos
- Selenium - Page Object Model
- Selenium - Page Factory
- Selenium - Record & Playback
- Selenium - Frameworks
- Selenium - Browsing Context
- Selenium Grid Tutorial
- Selenium - Overview
- Selenium - Architecture
- Selenium - Components
- Selenium - Configuration
- Selenium - Create Test Script
- Selenium - Test Execution
- Selenium - Endpoints
- Selenium - Customizing a Node
- Selenium Reporting Tools
- Selenium - Reporting Tools
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & other Technologies
- Selenium - Java Tutorial
- Selenium - Python Tutorial
- Selenium - C# Tutorial
- Selenium - Javascript Tutorial
- Selenium - Kotlin Tutorial
- Selenium - Ruby Tutorial
- Selenium - Maven & Jenkins
- Selenium - Database Testing
- Selenium - LogExpert Logging
- Selenium - Log4j Logging
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash Testing
- Selenium - Apache Ant
- Selenium - Github
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium Miscellaneous Concepts
- Selenium - Automation Frameworks
- Selenium - Keyword Driven Framework
- Selenium - Data Driven Framework
- Selenium - Hybrid Driven Framework
- Selenium - SSL Certificate Error
- Selenium - Alternatives
- Selenium Useful Resources
- Selenium - Quick Guide
- Selenium - Useful Resources
- Selenium - Automation Practice
- Selenium - Discussion
Selenium WebDriver - Browser Commands
Selenium Webdriver provides multiple commands which help to open a browser, perform some actions on an opened browser, and move out of the browser.
Some of the browser commands are discussed below −
driver.get("url of browser to be launched")
This command opens a browser then launches an application.
Syntax
driver.get("https://www.tutorialspoint.com/job_search.php");
driver.getTitle()
This command fetches the page title of the browser which is in focus as String. It accepts no parameters.
Syntax
String str = driver.getTitle();
driver.getCurrentUrl()
This command fetches the url of the browser which is in focus as String. It accepts no parameters.
Syntax
String str = driver.getCurrentUrl();
driver.getPageSource()
This command fetches the page source browser which is in focus as String. It accepts no parameters.
Syntax
String str = driver.getPageSource();
driver.close()
This command only closes the browser window in focus. It accepts no parameters and returns none.
Syntax
driver.close();
driver.quit()
This command closes everything associated with the driver session(browser and all background driver processes). It accepts no parameters and returns none.
Syntax
driver.quit();
Let us now discuss the identification of an element on a webpage shown in the below image. First, we would need to right click on the webpage, and then click on the Inspect button in the Chrome browser. Then, the corresponding HTML code for the whole page would be visible. For investigating a single element on a page, we would need to click on the left upward arrow, available to the top of the visible HTML code as highlighted below.
Let us take an example on the below page, where we would first launch an application having the URL: https://www.tutorialspoint.com/selenium/ and then obtain browser title Selenium Practice - Student Registration Form.
Please note, we can get the browser title of a web page in the HTML code, within the title tag which resides inside the head tag. In the below image, we see that the title of the page is Selenium Practice - Student Registration Form.
<title>Selenium Practice - Student Registration Form</title>
Then, we would click on the Login button, after which we would be navigated to another page having the browser title Selenium Practice - Login and url as: https://www.tutorialspoint.com/selenium/practice/login.php. Finally, we would quit the web driver session.
Syntax
// Initiate the Webdriver WebDriver driver = new ChromeDriver(); // launching a browser and open a URL driver.get("https://www.tutorialspoint.com/selenium/practice/login.php"); // Getting browser title after launch System.out.println("Getting browser title after launch: " + driver.getTitle()); // Getting browser URL after launch System.out.println("Getting URL after launch: " + driver.getCurrentUrl()); // Quitting browser driver.quit();
Example
Code Implementation on BrowserCommand.java class file.
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class BrowserCommand { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // launching a browser and open a URL driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php"); // Getting browser title after launch System.out.println("Getting browser title after launch: " + driver.getTitle()); // Getting browser URL after launch System.out.println("Getting URL after launch: " + driver.getCurrentUrl()); // identify link then click WebElement l = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a")); l.click(); // Getting browser title after clicking link System.out.println("Getting browser title after clicking link: " + driver.getTitle()); // Getting browser URL after launch System.out.println("Getting URL after clicking link: " + driver.getCurrentUrl()); // Quitting browser driver.quit(); } }
Output
Getting browser title after launch: Selenium Practice - Student Registration Form Getting URL after launch: https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php Getting browser title after clicking link: Selenium Practice - Login Getting URL after clicking link: https://www.tutorialspoint.com/selenium/practice/login.php Process finished with exit code 0
In the above example, we had launched a URL in the opened browser and obtained the browser title and current URL with the message in the console - Getting browser title after launch: Selenium Practice - Student Registration Form and Getting URL after launch: https://www.tutorialspoint.com/selenium/ respectively. We had then clicked on the Login and received the browser title and current URL after navigation with the message in the console - Getting browser title after clicking link: Selenium Practice - Login and Getting URL after clicking link: https://www.tutorialspoint.com/selenium/practice/login.php. . Next, we had to quit the driver session.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Let us take another example as shown in the below image where we would click on the New Tab button.
Then, on clicking the New Tab, we would get another window having the text New Tab.
Next, we would close the new window having the text New Tab and switch back to the original window and access the text - Browser Windows there. Finally, we would quit the session.
Syntax
WebDriver driver = new ChromeDriver(); // Get the window handle of the original window String oW = driver.getWindowHandle(); // get all opened windows handle ids Set<String> windows = driver.getWindowHandles(); // Iterating through all window handles for (String w : windows) { if(!oW.equalsIgnoreCase(w)) { // switching to child window driver.switchTo().window(w); // accessing element in new window WebElement e = driver.findElement(By.xpath("value of xpath")); System.out.println("Text in new window is: " + e.getText()); // closing the new window driver.close(); break; } } // switching to parent window driver.switchTo().window(oW); // accessing element in parent window WebElement e1 = driver.findElement(By.xpath("value of xpath")); System.out.println("Text in parent window is: " + e1.getText()); // quitting the browser session driver.quit();
Example
Code Implementation on WindowClose.java class file.
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.Set; import java.util.concurrent.TimeUnit; public class WindowClose { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Opening the webpage where we will open a new window driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php"); // click button and navigate to next window WebElement b = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/button[1]")); b.click(); // Get the window handle of the original window String oW = driver.getWindowHandle(); // get all opened windows handle ids Set<String> windows = driver.getWindowHandles(); // Iterating through all window handles for (String w : windows) { if(!oW.equalsIgnoreCase(w)) { // switching to child window driver.switchTo().window(w); // accessing element in new window WebElement e = driver.findElement (By.xpath("/html/body/main/div/div/h1")); System.out.println("Text in new window is: " + e.getText()); // closing new window driver.close(); break; } } // switching to parent window driver.switchTo().window(oW); // accessing element in parent window WebElement e1 = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/h1")); System.out.println("Text in parent window is: " + e1.getText()); // quitting the browser session driver.quit(); } }
Output
Text in new window is: New Tab Text in parent window is: Browser Windows Process finished with exit code 0
In the above example, we captured the text on the new window and received the message in the console - Text in new window is: New Tab. Then we closed the child window and switched back to the parent window. Lastly, obtain the text on the parent window in the console - TText in parent window is: Browser Windows.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Let us take another example of the above page, where we would get the page source using the getPageSource() method.
Syntax
// launching a browser and open a URL driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php"); // Getting browser page source System.out.println("Getting page source: " + driver.getPageSource());
Example
Code Implementation on WindowPagSource.java class file.
package org.example; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class WindowPagSource { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Opening the webpage where we will open a new window driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php"); // Getting browser page source System.out.println("Getting page source: " + driver.getPageSource()); // quitting the browser session driver.quit(); } }
In the above example, we captured the page source of the web page launched on a web page with the message in the console.
Thus, in this tutorial, we had discussed various browser commands using the Selenium Webdriver.
To Continue Learning Please Login
Login with Google