Selenium WebDriver Commands

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

Selenium WebDriver is an open-source web automation framework that is widely used in the software development industry for testing web applications. It allows testers to automate web-based tasks and create robust automated test scripts that can be used for continuous integration and delivery. Selenium WebDriver commands are a set of instructions that are used to interact with web elements in a web application. In this article, we will discuss the most commonly used Selenium WebDriver commands and their functionalities.

Types of WebDriver Commands in Selenium

1. Navigation Commands

Navigation commands are used to navigate to web pages, refresh pages, and manage browser history. The most commonly used navigation command is get(), which loads a new web page in the current browser window.

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.google.com")

The above code opens Google’s homepage in a new browser window. Another useful navigation command is back(), which navigates the browser back one page in history.

driver.back()

2. Element Interaction Commands

Element interaction commands are used to interact with web page elements such as buttons, text boxes, and drop-down menus. One of the most commonly used element interaction commands is click(), which simulates a click on a web page element.

from selenium import webdrive
driver = webdriver.Chrome()
driver.get("http://www.google.com")
search_box = driver.find_element_by_name("q")
search_box.send_keys("Python")
search_box.submit()
results = driver.find_elements_by_css_selector("h3")
for result in results:
   print(result.text)
driver.quit()

In the above code, we use the find_element_by_name() method to locate the search box on Google’s homepage. Then fill it with the word “Python,” and submit the search by pressing the Enter key. We then locate all the results on the search results page using the find_elements_by_css_selector() method and print their text to the console.

3. Browser Control Commands

Browser control commands are used to manage browser behavior, such as setting timeouts, managing cookies, and maximizing or minimizing the browser window. One of the most commonly used browser control commands is implicitly_wait(). It sets a default timeout for WebDriver to wait for an element to become available.

from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://www.google.com")
search_box = driver.find_element_by_name("q")
search_box.send_keys("Python")
search_box.submit()
driver.quit()

In the above code, we use implicitly_wait() to set a default timeout of 10 seconds. It enables WebDriver to wait for elements to become available on Google’s homepage.

Selenium WebDriver Commands

1. Selenium FindElement()

The FindElement() method is used to find a single web element on a web page. It takes a parameter called the “By” object, which can be used to locate the element using various attributes such as ID, Name, Class Name, Link Text, and XPath. Here is an example of how to use the FindElement() method to locate an element by its ID attribute:

WebElement element = driver.FindElement(By.Id("element_id"));

2. FindElements()

The FindElements() method is similar to the FindElement() method, except that it returns a list of all the matching elements on the web page. It also takes a “By” object as a parameter. Here is an example of how to use the FindElements() method to locate all the elements on a web page that have the same class name:

IList<WebElement> elements = driver.FindElements(By.ClassName("class_name"));

3. Click()

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

The Click() method is used to click on a web element. It is commonly used to submit forms, navigate links, and perform other actions on a web page. Here is an example of how to use the Click() method to click on a button:

WebElement button = driver.FindElement(By.Id("button_id"));
button.Click();

4. Selenium SendKeys()

The SendKeys() method is used to send text to a web element. It is commonly used to fill out form fields and input boxes. Here is an example of how to use the SendKeys() method to fill out a text box:

WebElement textBox = driver.FindElement(By.Id("textbox_id"));
textBox.SendKeys("Hello World");

5. Selenium Clear()

The Clear() method is used to clear the text from a text box or input field. It is commonly used to reset form fields. Here is an example of how to use the Clear() method to clear a text box:

WebElement textBox = driver.FindElement(By.Id("textbox_id"));
textBox.Clear();

6. Selenium GetAttribute()

The GetAttribute() method is used to get the value of a web element’s attribute. It takes a parameter called the “attribute name” and returns the value of that attribute. Here is an example of how to use the GetAttribute() method to get the value of an input field’s “value” attribute:

WebElement textBox = driver.FindElement(By.Id("textbox_id"));
string value = textBox.GetAttribute("value");

7. Selenum GetCssValue()

The GetCssValue() method is used to get the value of a CSS property of a web element. It takes a parameter called the “CSS property name” and returns the value of that property. Here is an example of how to use the GetCssValue() method to get the font-size of a web element:

WebElement element = driver.FindElement(By.Id("element_id"));
string fontSize = element.GetCssValue("font-size");

8. Selenium GetText()

The GetText() method is used to get the text value of a web element. It returns the text inside the element, without any HTML tags. Here is an example of how to use the GetText() method to get the text of a paragraph element:

WebElement paragraph = driver.FindElement(By.TagName("p"));
string text = paragraph.GetText();

9. Selenium IsDisplayed()

The IsDisplayed() method is used to check if a web element is displayed on the web page or not. It returns a boolean value, true if the element is displayed, and false if it is not. Here is an example of how to use the IsDisplayed() method to check if an element is visible on the page:

WebElement element = driver.FindElement(By.Id("element_id"));
bool isVisible = element.IsDisplayed();

10. Selenium IsEnabled()

The IsEnabled() method is used to check if a web element is enabled or disabled. It returns a boolean value, true if the element is enabled, and false if it is disabled. Here is an example of how to use the IsEnabled() method to check if a button is enabled:

WebElement button = driver.FindElement(By.Id("button_id"));
bool isEnabled = button.IsEnabled();

11. Selenium IsSelected()

The IsSelected() method is used to check if a web element is selected or not. It is commonly used to check the state of checkboxes and radio buttons. It returns a boolean value, true if the element is selected, and false if it is not. Here is an example of how to use the IsSelected() method to check if a checkbox is selected:

WebElement checkbox = driver.FindElement(By.Id("checkbox_id"));
bool isSelected = checkbox.IsSelected();

12. Selenium Navigate()

The Navigate() method is used to navigate to a web page. It takes a parameter called the “URL” and navigates the browser to that URL. Here is an example of how to use the Navigate() method to navigate to Google:

driver.Navigate().GoToUrl("https://www.google.com");

13. Selenium Quit()

The Quit() method is used to close the browser window and end the Selenium WebDriver session. It releases all the resources held by the browser instance. Here is an example of how to use the Quit() method to close the browser window:

driver.Quit();

14. Selenium SwitchTo()

The SwitchTo() method is used to switch between different windows, frames, and alerts on a web page. It allows testers to interact with different elements on the page that are not in the current context. Here is an example of how to use the SwitchTo() method to switch to an alert box:

IAlert alert = driver.SwitchTo().Alert();

15. Selenium Wait()

The Wait() method is used to wait for a specific condition to be met before proceeding with the test script. It can be used to wait for a web element to be visible, clickable, or disappear from the page. Here is an example of how to use the Wait() method to wait for a web element to be clickable:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
WebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("element_id")));

Syntax of Method

A method is a section of code that completes a particular task and can be called by other program elements. The syntax of a method varies depending on the programming language, but in general, it follows a common structure. Method syntax typically includes the following elements:

1. Method Type: The method type indicates what the method does or returns. For example, a method that adds two numbers together would have a type of int, indicating that it returns an integer value.

2. Method Name: The method name is the identifier used to call the method. It should be descriptive and follow naming conventions for the programming language being used.

3. Parameters: The parameters are the input values that are passed to the method. These can be any data type and can be optional or required depending on the method’s implementation.

4. Return Type: The return type is the data type of the value returned by the method. It can be any data type or void if the method does not return a value.

In Python, the syntax for a method looks like this:

def greet(name: str) -> str:
   return f"Hello, {name}!"

In this example, the method type is str, the method name is greet, and it takes one str parameter named name. The method returns a str value, which is a greeting string that includes the input parameter.

Conclusion

In conclusion, Selenium WebDriver commands are a set of instructions that allow testers to interact with web elements in a web application. They are essential for creating robust automated test scripts that can be used for continuous integration and delivery. The most commonly used Selenium WebDriver commands include FindElement(), FindElements(), Click(), SendKeys(), Clear(), GetAttribute(), GetCssValue(), GetText(), IsDisplayed(), IsEnabled(), IsSelected(), Navigate(), Quit(), SwitchTo(), and Wait(). By mastering these commands, testers can create powerful automated test scripts that can be used to test web applications efficiently and effectively.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

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