Selenium WebDriver Browser Commands

We offer you a brighter future with FREE online courses - Start Now!!

Selenium WebDriver is a powerful tool for automating web browsers. It provides a range of browser commands that allow you to interact with the browser and control its behavior during test execution. In this article, we’ll explore the different types of browser commands available in Selenium WebDriver. We will also learn how they can be used to control the browser during test execution.

Types of Browser Commands in Selenium WebDriver

There are several types of browser commands available in Selenium WebDriver. Let’s explore each of these types of commands in detail.

1. Selenium Navigation Commands

Navigation commands allow you to control the browser’s navigation and move from one web page to another. The following are the navigation commands available in Selenium WebDriver:

a. get(url)

The get(url) command is used to navigate to a new web page by providing its URL as an argument.

// Navigate to a new web page
driver.get("https://www.example.com");
b. navigate().to(url)

The navigate().to(url) command is similar to the get(url) command, but it is provided by the Navigation interface of the WebDriver API.

// Navigate to a new web page
driver.navigate().to("https://www.example.com");
c. navigate().back()

The navigate().back() command is used to move back to the previous web page in the browser’s history.

// Navigate back to the previous web page
driver.navigate().back();
d. navigate().forward()

The navigate().forward() command is used to move forward to the next web page in the browser’s history.

// Navigate forward to the next web page
driver.navigate().forward();
e. navigate().refresh()

The navigate().refresh() command is used to reload the current web page.

// Reload the current web page
driver.navigate().refresh();

2. Selenium Browser Window Commands

Browser window commands allow you to control the browser window’s behavior during test execution. The following are the browser window commands available in Selenium WebDriver:

a. manage().window().maximize()

The manage().window().maximize() command is used to maximize the browser window.

// Maximize the browser window
driver.manage().window().maximize();
b. manage().window().fullscreen()

The manage().window().fullscreen() command is used to make the browser window full screen.

// Make the browser window full screen
driver.manage().window().fullscreen();
c. manage().window().setSize(width, height)

The manage().window().setSize(width, height) command is used to set the size of the browser window.

// Set the size of the browser window
driver.manage().window().setSize(new Dimension(800, 600));

3. Selenium Alert Commands

Alert commands allow you to handle alerts that may appear during test execution. The following are the alert commands available in Selenium WebDriver:

a. switchTo().alert().accept()

The switchTo().alert().accept() command is used to accept an alert that appears on the web page.

// Accept an alert
driver.switchTo().alert().accept();
b. switchTo().alert().dismiss()

The switchTo().alert().dismiss() command is used to dismiss an alert that appears on the web page.

// Dismiss an alert
driver.switchTo().alert().dismiss();
c. switchTo().alert().getText()

The switchTo().alert().getText() command is used to get the text of an alert that appears on the web page.

// Get the text of an alert
String alertText = driver.switchTo().alert().getText();
d. switchTo().alert().sendKeys(text)

The switchTo().alert().sendKeys(text) command is used to enter text into a prompt alert that appears on the web page.

// Enter text into a prompt alert
driver.switchTo().alert().sendKeys("Hello, World!");

4. Selenium Frame Commands

Frame commands allow you to switch between frames that may be present on a web page. The following are the frame commands available in Selenium WebDriver:

a. switchTo().frame(index)

The switchTo().frame(index) command is used to switch to a frame on a web page using its index.

// Switch to a frame using its index
driver.switchTo().frame(0);
b. switchTo().frame(nameOrId)

The switchTo().frame(nameOrId) command is used to switch to a frame on a web page using its name or ID.

// Switch to a frame using its name or ID
driver.switchTo().frame("frame_name_or_id");
c. switchTo().defaultContent()

The switchTo().defaultContent() command is used to switch back to the default content of the web page after switching to a frame.

// Switch back to the default content of the web page
driver.switchTo().defaultContent();

5. Cookies Commands in Selenium

Cookies commands allow you to manage cookies that are stored by the web browser during test execution. The following are the cookies commands available in Selenium WebDriver:

a. manage().addCookie(cookie)

The manage().addCookie(cookie) command is used to add a new cookie to the browser.

// Add a new cookie to the browser
Cookie cookie = new Cookie("name", "value");
driver.manage().addCookie(cookie);
b. manage().getCookies()

The manage().getCookies() command is used to get all the cookies stored by the browser.

// Get all the cookies stored by the browser
Set<Cookie> cookies = driver.manage().getCookies();
c. manage().getCookieNamed(name)

The manage().getCookieNamed(name) command is used to get a specific cookie stored by the browser using its name.

// Get a specific cookie stored by the browser using its name
Cookie cookie = driver.manage().getCookieNamed("cookie_name");
d. manage().deleteCookie(cookie)

The manage().deleteCookie(cookie) command is used to delete a specific cookie stored by the browser.

// Delete a specific cookie stored by the browser
Cookie cookie = driver.manage().getCookieNamed("cookie_name");
driver.manage().deleteCookie(cookie);
e. manage().deleteAllCookies()

The manage().deleteAllCookies() command is used to delete all cookies stored by the browser.

// Delete all cookies stored by the browser
driver.manage().deleteAllCookies();

6. JavaScript Commands

JavaScript commands allow you to execute JavaScript code during test execution. The following are the JavaScript commands available in Selenium WebDriver:

a. executeScript(script, args)

The executeScript(script, args) command is used to execute JavaScript code on the web page during test execution.

// Execute JavaScript code on the web page during test execution
String script = "return document.title;";
String title = (String) driver.executeScript(script);
b . executeAsyncScript(script, args)

The executeAsyncScript(script, args) command is used to execute asynchronous JavaScript code on the web page during test execution.

// Execute asynchronous JavaScript code on the web page during test execution
String script = "var callback = arguments[arguments.length

7. Get Command

The “get” command in WebDriver browser commands is used to navigate the browser to a specified URL. It takes a single argument, which is the URL of the page you want to load. Once the page is loaded, the browser will remain on that page until further instructions are given.

Here is an example of using the “get” command with WebDriver in Python:

from selenium import webdriver

browser = webdriver.Chrome()

browser.get('https://www.google.com')

browser.implicitly_wait(10)

search_bar = browser.find_element_by_name('q')
search_bar.send_keys('webdriver')
search_bar.submit()

browser.quit()

In the example above, we first create a new instance of the Chrome browser using the webdriver.Chrome() command. We then use the get() command to navigate to Google’s homepage. After waiting for the page to load, we search for the keyword “webdriver” in the search bar and submit the search. Finally, we close the browser using the quit() command.

8. Get Title Command

The “get title” command in WebDriver browser commands is used to retrieve the title of the current web page that is loaded in the browser. It returns a string that represents the title of the page.

Here is an example of using the “get title” command with WebDriver in Python:

from selenium import webdriver

browser = webdriver.Chrome()

browser.get('https://www.google.com')

browser.implicitly_wait(10)

page_title = browser.title
print('Page title:', page_title)

browser.quit()

In the example above, we first create a new instance of the Chrome browser using the webdriver.Chrome() command. We then use the get() command to navigate to Google’s homepage. After waiting for the page to load, we retrieve the page title using the title attribute and store it in a variable called page_title. Finally, we print the page title to the console and close the browser using the quit() command.

9. Selenium Get Page Source Command

The “get page source” command in WebDriver browser commands is used to retrieve the HTML source code of the current web page that is loaded in the browser. It returns a string that represents the HTML source code of the page.

Here is an example of using the “get page source” command with WebDriver in Python:

from selenium import webdriver

browser = webdriver.Chrome()

browser.get('https://www.google.com')

browser.implicitly_wait(10)

page_source = browser.page_source
print('Page source:', page_source)

browser.quit()

In the example above, we first create a new instance of the Chrome browser using the webdriver.Chrome() command. We then use the get() command to navigate to Google’s homepage. After waiting for the page to load, we retrieve the page source using the page_source attribute and store it in a variable called page_source. Finally, we print the page source to the console and close the browser using the quit() command.

10. Selenium Quit Command

The “quit” command in WebDriver browser commands is used to close the browser window and end the WebDriver session. It is important to use the quit() command at the end of the script to ensure that all browser windows and sessions are properly terminated and any resources used by the WebDriver instance are freed up.

Here is an example of using the “quit” command with WebDriver in Python:

from selenium import webdriver

browser = webdriver.Chrome()

browser.get('https://www.google.com')

browser.implicitly_wait(10)

search_bar = browser.find_element_by_name('q')
search_bar.send_keys('webdriver')
search_bar.submit()

browser.quit()

In the example above, we first create a new instance of the Chrome browser using the webdriver.Chrome() command. We then use the get() command to navigate to Google’s homepage. After waiting for the page to load, we search for the keyword “webdriver” in the search bar and submit the search. Finally, we close the browser using the quit() command. This ensures that the browser window and the WebDriver session are properly terminated and any resources used by the WebDriver instance are freed up.

Conclusion

In conclusion, browser commands in Selenium WebDriver are essential for automated web testing. With these commands, you can control and manage the browser during test execution. You can interact with web elements, handle alerts and prompts, and execute JavaScript code. Knowing these commands and their usage can help you write more efficient and effective tests. Also, you can troubleshoot issues that may arise during test execution.

Although Selenium WebDriver’s most popular browser commands have been covered in this article, there are many more commands and functions accessible. Therefore, it is important to continue learning and exploring the capabilities of Selenium WebDriver to become an expert in automated web testing.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *