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:
These are the attributes available for By class:
P.S: If you asked yourself "Why CSS selector before XPath ?"
The answer is: performance...
Why do we need to use different locators?
- - Sometimes developers may not provide all locators for all elements.
- - 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
driver.find_element_by_tag_name("button")
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.
Radio button:
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
selenium knows how to make an action only inside browsers... so what can we do ?
Answer: sendkeys()
such a great tutorial
ReplyDelete