Web Element Operations

Everything on the website is an web element, normally we elements like textbar, button, menus, submenus, radio button, headers, footer, links, plain texts, forms so on.

when we do perform tests manually we tend to click elements and will observe the behaviors, and we will set the values and we will observe the conditions and we will have our own check points.

Selenium also provides capability to test these kind of actions, we can also retrieve the values from the elements to check colors and sizes as selenium cannot perform visual testing.

Click in selenium python bindings

We can perform clicking operation using selenium python, before performing the click operation we have to uniquely find the elements using locators

The height and width of element must be more than 0px to click the element in selenium python

If element is not enabled and if you perform a click on it, selenium throws InvalidElementStateException

Below program perfoms a click on the 'About' link on the google page at the bottom


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								driver.find_element_by_link_text("About").click()			
								

Sendkeys in selenium python

sendKeys method sets text into an editable elements (textbar,text area, button) without replacing the previously available content.

If you use more than one send keys on a field then selenium appends all of them, selenium will not replace existing value when we use sendKeys.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								driver.find_element_by_name("q").send_keys("Chercher Tech")			
								

Clear an element in selenium python

If we want to set values to a textfield we have to clear the field and set the value using sendkeys.

clear() method will help the user to clear the field on the webpage, if we donot clear a field then also selenium will not throw any exception but it just appends the new value with old value.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								driver.find_element_by_name("q").send_keys("Chercher Tech")
								# clear the field
								driver.find_element_by_name("q").clear()
								# set the value
								driver.find_element_by_name("q").send_keys("CherTech")		
								

Submit

submit() method in selenium python submits a form, user can use it only along with form or with form element only.

submit() method clicks a button is a misconception, it works only when that particular button is associated with form.


								# submit the form
								driver.find_element_by_name("value").submit()		
								

Get Text of an Element in Python Selenium

text variable present in the driver will fetch the values of the text, not only current element text but also the text of other elements inside that element.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								# get the text
								print("Text retrieved : "+ driver.find_element_by_link_text("About").text)	
								


								Output : Text retrieved : About	
								

Get Attribute of an Element in selenium python

get_attibute() method fetches the value of an attribute, in html code whatever is present in left side of '=' is attribute, value on the right side is Attribute value


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								# get the title attribute
								print("Title of searchbar : "+ driver.find_element_by_id("q").get_attribute("title"))
								


								Output : Title of searchbar : Search
								

Insight about get Attributes:

Below are few insights from my experience about the get attribute

1. Selenium return the attribute present at the time of query, if the attribute value changes after the query then selenium will not return the new value

2. getAttibute() returns blank as value if attribute is not set to any value (exception for boolean values).

3. getAttibute() returns 'true' incase if you are searching for boolean value, and the value is not set

Few Boolean attribute examples : checked, readonly, required, multiple, complete so on.

4. getAttibute() returns 'null' if there no such attribute

Get CSS Value in selenium python

value_of_css_property method in selenium python fetches the value of an css property of an webelement in selenium python bindings, we have to pass css property, which selenium binding have to fetch.

Whenever you want retrieve any css property you should check the property name in the Computed tab and pass the value to the method.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								# get the css value
								cssValue = driver.find_element_by_name("q").value_of_css_property("font-size")
								print("font size searchbar : "+ cssValue)

								


								Output : font size searchbar : 16px
								

Css values are depends on the browser, you may not get same values for all the browser, below are few such examples

Few browsers may show 1px and other may show 1 px [ there is space in difference ]

Few browser may show rgba(0, 123, 255, 1) and other may show rgb(0, 123, 255) [ rgb and rgba difference]

Get Size of an Element in selenium python

size property present in selenium determines the size of an element, size consists of two values width and height which are sum of respective attributes

Width = margin-left + margin-right + padding-left + padding-right + actual width; width get size in selenium webdriver

Height = margin-top + margin-bottom + padding-top + padding-bottom + actual height; height get size in selenium webdriver

Above images are only for info purpose, size variable returns the dictionary containing height a


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								# get the css value
								sizeOfElement = driver.find_element_by_name("q").size
								print("Width of the searchbar : ", sizeOfElement["width"])
								print("Height of the searchbar : ", sizeOfElement["height"])
								


								Output : 
								Width of the searchbar :  403
								Height of the searchbar :  34
								

is_displayed in selenium python

is_displayed() method in selenium webdriver verifies and returns a boolean value based on the state of the element whether it is displayed or not.

If there is no such attribute as hidden, selenium binding considers that the element is displayed (visit Boolean Attribute for details) and returns true.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								# get the css value
								isDisplayed = driver.find_element_by_name("q").is_displayed()
								print("Is Searchbar Displayed : ", isDisplayed)
								


								Output : Is Searchbar Displayed :  True
								

is_enabled in selenium python

is_enabled() method in selenium python verifies and returns a boolean value based on the state of the element whether it is enabled or not.

If there is no such attribute as enabled, selenium webdriver considers that the element is enabled (visit Boolean Attribute for details) and returns true.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								# element enabled
								isEnabled = driver.find_element_by_name("q").is_enabled()
								print("Is searchbar enabled : ", isEnabled)
								


								Output : Is searchbar enabled :  True
								

is_selected in selenium python

is_selected() verifies if an element is selected or not. is_selected() method returns boolean value, true if the element is selected and false if it is not.

It is widely used on check boxes, radio buttons and dropdowns in selenium webdriver.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("https://chercher.tech/selenium-webdriver-sample")
								# get the css value
								isSelected = driver.find_element_by_xpath("//input[@id='selected']").is_selected()
								print("Is checkbox selected : ", isSelected)
								


								Output : Is checkbox selected : ", False
								

Location of element in selenium python

location property in selenium bindings fetches the x and y coordinates of the element. This property returns the dictionary as return value.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								# get location
								locate = driver.find_element_by_name("q").location
								print("x value: ", locate["x"])
								print("y value: ", locate["y"])
								


								Output : 
								x value:  290
								y value:  323

								

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech