Selenium Web Driver Commands

Posted By :Nidhi Ojha |28th May 2019

In previous blogs we learnt launching browswers in selenium. Now in this blog, we will learn some of the basic selenium commands for performing operations like opening a URL, clicking on buttons, writing in textbox, closing the browser etc. For our practice, a dummy webpage with different types of web elements is available. Let's see the basic commands of Selenium WebDriver.

 

1.Opening a URL:
a. Using Get method-

The driver.get() method does the task of navigating to a web page by passing the string URL as parameter. Syntax-

driver.get("https://technologies.oodles.io/");

b. Using Navigate method-

The driver.navigate().to() method is used to open a web page like driver.get() method.  Syntax-

driver.navigate().to("https://technologies.oodles.io/");

 

2. Clicking on WebElements:
The click() method in Selenium does the task of performing the click operation on web elements. The click() method is called on the recognised webElements to conduct the click operation.

//Clicking an element directly driver.findElement(By.id("button1")).click(); //Or by first creating a WebElement then applying click() operation WebElement submitButton = driver.findElement(By.id("button2")); submitButton.click();

 

3. Writing in a Textbox:
The sendKeys() method is used for writing in a textbox or any element of text input type.

//Creating a textbox webElement WebElement element = driver.findElement(By.name("q")); //Using sendKeys to write in the textbox element.sendKeys("Oodles Technologies!");

 

4. Clearing text in a Textbox:
The clear() method is used to clear the text written in a textbox or any web element of text input type.

//Clearing the text written in text fields driver.findElement(By.name("q")).clear();

 

5. Fetching text written over any web element:
In automation, we frequently need to get text put over a web element to do assertions or debugging. So for this, we have getText() method in selenium webDriver.

//Fetching the text written over web elements driver.findElement(By.id("element123")).getText();

 

6. Navigating backwards in a browser:
Selenium gives navigate().back() command to move backwards in the browser's history.

//Navigating backwards in browser driver.navigate().back();

 

7. Navigating forward in a browser:
Selenium gives navigate().forward() command to move forward in a browser.

//Navigating forward in browser driver.navigate().forward();

 

8. Refreshing the browser:
There are many ways to refresh a page in Selenium WebDriver-

Using driver.navigate().refresh() command
Using sendKeys(Keys.F5) on any textbox on the webpage
Using driver.get("URL") with current URL
Using driver.navigate().to("URL") with current URL

//Refreshing browser using navigate().refresh() driver.navigate().refresh(); //By pressing F5 key on any textbox element driver.findElement(By.id("id123")).sendKeys(Keys.F5); //By opening the current URL using get() method driver.get("https://www.oodlestechnologies.com"); //By opening the current URL using navigate() method driver.navigate().to("https://www.oodlestechnologies.com");

 

9. Closing browser:
Selenium gives two commands to close browsers close() and quite(). The driver.close() command does the task of closing the browser having focus. Whereas, the driver.quite command can be used to close all the browser instances open.

//To close the current browser instance driver.close(); //To close all the open browser instances driver.quit();


About Author

Nidhi Ojha

Nidhi is certified in Manual Testing and SQL. She has done B.tech in Electronics and Communication branch, apart from this she loves to travel to different places.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us