Selenium - locators


Selenium Locators are used for identifying the web elements on the web page. To access the HTML elements from a web page locators are used.

In Selenium, we can use locators to perform actions on text boxes, checkboxes, links, radio buttons, list boxes, and other web elements. Locators help us in identifying the objects.

This tutorial explains different types of locators, how, when, and ideal Strategies to use these locators.


Example of web browser methods:


Find Element by:

There are two private methods that might be useful for locating page elements:

  • find_element
  • find_elements

Example usage:

from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')

These are the attributes available for By class:

ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"


Difference between FindElement and FindElements:



Locator types list:



P.S: If you asked yourself "Why CSS selector before XPath ?"

The answer is: performance...



Where we can find the elements ?

Right-click on the above web element to inspect the element.








At the core of Selenium is WebDriver, an interface to write instruction sets that can be run interchangeably in many browsers. Here is one of the simplest instructions you can make:




Why do we need to use different locators?

  1. -  Sometimes developers may not provide all locators for all elements.
  2. -  Some locators may be duplicated sometimes.

So we have to choose any one unique locator to identify the element. Let’s look into these selenium locators one by one.


HTML Remainder







Find element by id:

The below code points to the element which has id as pancakes

driver.find_element_by_id("pancakes")

Find element by name:

The below code tries to find the element which has a name as Ban.

driver.find_element_by_name("Ban")

Find element by class:

driver.find_element_by_class_name("Banana")


Find element by linkText OR Find element by PartialText:

We can find the element using the hyperlink text present in the link element; we can either use partial text or Link text methods to find the element.

  • Use find_element_by_link_text method to find elements when the hyperlink is static
  • Use find_element_by_partial_link_text method when a certain part of the string keeps changing.
# finds element based full match
driver.find_element_by_link_text("Selenium Webdriver")

#find element based on partial text
driver.find_element_by_partial_link_text("Webdriver")


Find element by Tag:

Tagname is nothing but the item used or a tag used to form that particular element; This method returns the first matching element, if there is no match then raises a NoSuchElementException

driver.find_element_by_tag_name("button")


Submit:

driver.find_element_by_id("next").submit()

Submit VS Click:

driver.find_element_by_id("next").click()

  • Click can click inside & outside a form.

  • Submit live only inside a form.



The submit() function is there to make life easier. You can use it on any element inside of form tags to submit that form.

You can also search for the submit button and use click().

So the only difference is that click() has to be done on the submit button and submit() can be done on any form element. It's up to you...

 

SendKeys:

Keyboard represents a KeyBoard event. KeyBoard actions are performed by using low-level interface which allows us to provide virtualized device input to the web browser.





Update Text:

Simply clear() the text, and start over again:




Radio button:






driver.find_element_by_xpath("//input[@value='Yes']").click()

Checkbox:

driver.find_element_by_xpath(//input[@name='checkbox']).click()


Note:

Radio button = only 1 can be chosen.

CheacBox = multiple chooses


Select class:

Selenium provides a convenient Select class to work with select -> option constructs:

for more information:

https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.select




How to get a list of values from dropdown list ?




Upload file

selenium knows how to make an action only inside browsers... so what can we do ?

Answer: sendkeys()







Comments

Post a Comment

Popular posts from this blog

Selenium Webdriver Manager

Selenium - brief

אבטחת מידע

Page Object Model, what is it ?

תבניות עיצוב - מפעל

Security Informtion