Webelement commands in Selenium Python
In the previous articles on Selenium Python Tutorial, we have covered “Synchronization in Selenium Python“. In this tutorial, we will learn Webelement commands in Selenium Python.
Let us discuss some of the common Webelement commands used in Selenium Python. With the help of the web element commands, we can successfully perform actions on elements and also validate its behavior and other characteristics.
What is WebElement?
WebElement in Selenium represents an HTML element. It basically represents a DOM element in a HTML document.
WebElement Commands in Selenium Python
The following are some of the most commonly used WebElement commands in Selenium WebDriver.
send_keys ()
This is a method that is used to input text in an edit box, text area, fields inside the form, and so on. This method can also be used for setting up file inputs and to send modifier keys from the Keys class.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage- to-automate/") # to enter text in edit box driver.find_element_by_name ("password").send_keys ("Test")
click ()
This is a method that is used to click on a link or button. The element to be clicked is specified in the parameter of the method. If no parameter is mentioned, the present mouse position is clicked.
InvalidElementException is thrown if we try to click on an element in a disabled state.
Code Implementation with click () method.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/") # to click on a link driver.find_element_by_link_name ("Blog").click ()
clear ()
This is a method that is used to clear the text from an edit box or a text area. Once the existing text is cleared, send_keys () method is applied to again input our desired text. Code Implementation with a clear () method.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # to enter text in edit box driver.find_element_by_name ("password").send_keys ("Test") # to clear the input text in edit box driver.find_element_by_name ("password").clear () # to re-enter text in edit box driver.find_element_by_name ("password").send_keys ("Testing")
submit ()
This is a method that can be used with any element within the form tag. It waits to complete the page load and submit the form. Code Implementation with submit () method.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # to enter text in edit box driver.find_element_by_name ("password").send_keys ("Test") # to submit the form driver.find_element_by_name ("password").submit ()
text
This is used to fetch the inner text of an element. It mainly takes care of the text displayed on the screen with its sub-elements without considering the leading and trailing spaces. Code Implementation with text.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # identify the element and get the text print (driver.find_element_by_css_selector ("h2").text) # to close the browser driver.close ()
get_attribute ()
This method is used to get the attribute or property value of an element. This method identifies the key-value pair in an HTML document. If a value is not set for an attribute in the HTML document, none is retrieved by the method. In case an attribute value is in Boolean, the get_attribute () method gives true or false. The attribute which we want to fetch is passed as an argument to the method. Code Implementation with get_attribute ().
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # to identify the element I = driver.find_element_by_css_selector ("h2") # to get the style attribute of the element print ( "Attribute style is: " + l.get_attribute ("style") ) # to close the browser driver.close ()
value_of_css_property ()
This method is used to get the value of the CSS property of the element. The method accepts the name of the CSS property for which we want to get the value. Code Implementation with value_of_css_property ().
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # to identify the element I = driver.find_element_by_css_selector ("h2") # to get the color css property of the element print ( "Css property color is: " + l.value_of_css_property ("color") ) # to close the browser driver.close ()
is_displayed ()
This method is used to check if the web element is displayed on the page or not. This method gives a Boolean value of either true or false. Sometimes an element contains an attribute as hidden. In that case, Selenium considers that element is not visible on the page and returns false. Code Implementation with is_displayed ().
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # to identify the element I = driver.find_element_by_css_selector ("h2") # to verify if the element is displayed with is_displayed() print ( "The element is displayed (true/false): " + l.is_displayed ()) # to close the browser driver.close ()
is_enabled ()
This method is used to check if the web element is enabled or not. This method gives a Boolean value of either true or false depending on the condition of the element. If an element does not contain the enabled attribute, the is_enabled () method gives true value. Code Implementation with is_enabled ().
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # to identify the element I = driver.find_element_by_name ("spbutton") # to verify if the element is enabled with is_enabled () print ( "The element state is (true/false): " + l.is_enabled ()) # to close the browser driver.close ()
is_selected ()
This method is used to check if the web element is selected or not. This method gives a Boolean value of either true or false depending on the condition of the element. This method is used for radio buttons, checkboxes, and dropdowns, and so on. Code Implementation with is_selected ().
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # to identify the element I = driver.find_element_by_xpath ("//input[@value = 'cbselenium']") # to verify if the element is selected with is_selected () print ( "The element state is (true/false): " + l.is_selected ()) # to close the browser driver.close ()
location
This method is used to get the web element location in terms of x and y coordinates. Code Implementation with location method.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # to identify the element I = driver.find_element_by_xpath ("//input[@value = 'cbselenium']") # to get the location of the element loc = l.location print ( "Coordinate x of the element is : " + loc["x"]) print ( "Coordinate y of the element is : " + loc["y"]) # to close the browser driver.close ()
In the next article, we will learn How To Handle Child Window, Frames, Alerts in Selenium Python
Related posts:
- Selenium Python Tutorial
- Selenium Java Tutorial
- Selenium Interview Questions
- API Testing Tutorial
- Postman Tutorial