Element Methods in Selenium Python


Selenium, an open source automation testing tool is used with other programming scripts like python , java , javascript and pearl to test web applications. It is widely used by developers for automation testing.

Element Methods of Selenium

1. send_keys () −

Used for setting up input text boxes which includes edit box, text area, fields inside forms and modifier keys. It extends properties from the keys class.

Return Type − null

2. is_selected() −

Checks if an element is selected or not by the user

Return Type − boolean value ( True or False).

3. is_displayed() −

Checks if the element is visible to the user.

Return Type − boolean value ( True or False).

4. get_property() −

Used to get properties of an element

Return Type − Value of property

5. is_enabled() −

Used to check if an element is enabled or not.

Return Type − boolean value ( True or False).

6. click() −

Used to click a link or a button. The attribute is mentioned over the button itself

Return Type − directs you to the path anchored with the button element if there exists an URL. else throws InvalidElementException.

7.text() −

Traces the occurrence of the given input text by partial matching using Xpath scanner.

Return Type − returns the line of text containing the input text as a whole or in parts.

8.location() −

Location method is used to retrieve the location of an accessible search field.

Return Type − Coordinates or dimensions of the element in the form of a dictionary.

9.screenshot() −

The screenshot method allows the user to save a screenshot of the current element.

Return Type − True if the screenshot was successful, false if there’s an IO Error.

Example 1

The following example illustrates the use of properties and directs to tutorialspoint websites using the get() method.

Algorithm

  • Import the required webdriver module from selenium

  • Create a webdriver object with Chrome() for windows users and Safari() for Mac users.

  • Fetch the url to be opened and retrieve data.

  • It’s optional to use the above selenium methods for advanced use.

  • Close the webdriver object.

# import webdriver
from selenium import webdriver
 
# create webdriver object - Windows User
driver = webdriver.Chrome()

#create webdriver object - Mac Users
#Change the settings in the Safari Develop menu to Allow Remote Automation
driver = webdriver.Safari()
driver.get("https://www.tutorialspoint.com/")  

driver.close()

Output

Make sure to change the settings to ‘Allow Remote Automation’ in the develop menu if using Mac OS.

Example 2

The following example illustrates the use of screenshot() and takes a screenshot of the current element.

Algorithm

  • Import the required webdriver module from selenium.

  • Create a webdriver object with Chrome() for windows users and Safari() for Mac users.

  • Fetch the url to be opened and retrieve data.

  • Use element.screenshot() and mention the path where the image has to be stored within parentheses.

  • Close the webdriver object.

# import webdriver
from selenium import webdriver
 
# create webdriver object - Windows User
driver = webdriver.Chrome()

#create webdriver object - Mac Users
#Change the settings in the Safari Develop menu to Allow Remote Automation
driver = webdriver.Safari()
driver.get("https://www.tutorialspoint.com/")  

element = driver.find_element_by_class_name("header--navbar")
 
# click screenshot
element.screenshot('C:\Users\Swetha\Pictures\Screenshots\header.png')

driver.close()

Output

You can view the image in the folder that was mentioned in the path.

Conclusion

It finds maximum application in automatic detection of results to the tests which are predefined by the user. In simple terms, it can be called as automation testing which is done efficiently with Selenium webdriver. The ideology behind the creation of this module is to test UI for applications in a short time span such that it satisfies the constraints put forward by the client.

Updated on: 23-Aug-2023

246 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements