How to locate elements in Python Selenium Webdriver
There are 8 different strategies available in Selenium WebDriver to locate elements in a page.
Using,
- id
- name
- class name
- tag name
- link text
- partial linktext
- tag name
- xpath and
- css
Any one of these strategies can be used based on the usecase.
Python Selenium WebDriver implementation provides the following methods to find or locate elements in a page.
If there are more than one element match, first element will be returned.
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
Following methods are also available in Python Selenium WebDriver to find multiple elements.
These methods will return list of elements.
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
Locating element by Id
This strategy can be used when id attribute of an element is known.
If there are no element with matching id attribute, NoSuchElementException will be thrown.
For instance, consider this page source:
<html> <body> <form id="registerationForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="confirmPassword" type="password" /> <input name="email" type="text" /> <input name="continue" type="submit" value="Register" /> </form> </body> <html>
The form element can be located using id attribute like this:
formElement = driver.find_element_by_id('registerationForm')
As we seen already, if there are multiple match, the first element with the id attribute value matching will be returned.
Locating by Name
This strategy can be used when name attribute of an element is known.
If there are no element with matching name attribute, NoSuchElementException will be thrown.
For instance, consider this page source:
<html> <body> <form id="registerationForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="confirmPassword" type="password" /> <input name="email" type="text" /> <input name="continue" type="submit" value="Register" /> </form> </body> <html>
The username, password, confirmPassword, email and continue elements can be located using name attribute.
For example, username element can be located like this:
username = driver.find_element_by_name('username')
Locating Hyperlinks by Linktext and Partiallinktext
This strategy can be used when link text used within an anchor tag (<a>) is known.
This method will return the first element with the link text value matching the location.
If there is no match, a NoSuchElementException will be raised.
For instance, consider this page source:
<html> <body> <a href="http://allselenium.info/how-to-open-firefox-browser-by-passing-driver-executable-path/">Open Firefox browser by passing driver executable path</a> <a href="http://allselenium.info/how-to-open-url-in-specific-firefox-browser-version/">Open URL in specific Firefox browser version</a> </body> <html>
Links in this page can be located like this:
link1 = driver.find_element_by_link_text('Open Firefox browser by passing driver executable path') link2 = driver.find_element_by_partial_link_text('Open Firefox browser')
link = driver.find_element_by_partial_link_text('Open')
This will match both the links and first link will be returned.
Locating Elements by Tag Name
This strategy can be used when you want to locate an element by tag name. First element with the given tag name will be returned.
If no element has a matching tag name, a NoSuchElementException will be raised.
For instance, consider this page source:
<html> <body> <strong>Privacy Policy</strong> <p>Content</p> </body> <html>
‘Privacy Policy’ element can be located like this:
strong = driver.find_element_by_tag_name('strong')
Locating Elements by Class Name
This strategy can be used when class attribute of an element is known.
If there are no element with matching class attribute, NoSuchElementException will be thrown.
For instance, consider this page source:
<html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input class="login-btn" name="login" type="submit" value="Login" /> </form> </body> <html>
Login element can be located like this:
login = driver.find_element_by_class_name('login-btn')
Please let us know your thoughts in comment section. Follow our article ‘Working with XPath locator‘ to understand building XPath locators.
One Response
[…] let us know your thoughts in comment section. Please follow our article to check how to handle elements using other locator […]