Locators Strategies in Selenium Python are methods that are used to locate elements from the page and perform an operation on the same. Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using Selenium WebDriver. After one has installed selenium and checked out – Navigating links using get method, one might want to play more with Selenium Python. After opening a page using selenium such as neveropen, the user might want to click some buttons automatically or fill a form automatically or any such automated task. This article revolves around Locating single elements in Selenium Python.
Locator Strategies to locate single first elements
Note: find_element_by_*… has been deprecated and hence By class is used now with find_element. To know more: Visit here.
Selenium Python follows different locating strategies for elements. For locating elements, you have to import By
from selenium.webdriver.common.by import By
One can locate an element in 8 different ways. Here is a list of locating strategies for Selenium in python –
Locators | Description |
---|---|
By.ID | The first element with the id attribute value matching the location will be returned. |
By.NAME | The first element with the name attribute value matching the location will be returned. |
By.XPATH | The first element with the xpath syntax matching the location will be returned. |
By.LINK_TEXT | The first element with the link text value matching the location will be returned. |
By.PARTIAL_LINK_TEXT | The first element with the partial link text value matching the location will be returned. |
By.TAG_NAME | The first element with the given tag name will be returned. |
By.CLASS_NAME | the first element with the matching class attribute name will be returned. |
By.CSS_SELECTOR | The first element with the matching CSS selector will be returned. |
By.ID
With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a NoSuchElementException will be raised.
Syntax:
driver.find_element(By.ID, "id_of_element")
Example: For instance, consider this page source:
html
< html > < body > < form id="loginForm"> < input name="username" type="text" /> < input name="password" type="password" /> < input name="continue" type="submit" value="Login" /> </ form > </ body > < html > |
Now after you have created a driver, you can grab an element using –
login_form = driver.find_element(By.ID, 'loginForm')
Note – find_element_by_id() has been deprecated.
By.NAME
With this strategy, the first element with the name attribute value matching the location will be returned. If no element has a matching name attribute, a NoSuchElementException will be raised.
Syntax:
driver.find_element(By.NAME, "name_of_element")
Example: For instance, consider this page source:
html
< html > < body > < form id="loginForm"> < input name="username" type="text" /> < input name="password" type="password" /> < input name="continue" type="submit" value="Login" /> </ form > </ body > < html > |
Now after you have created a driver, you can grab an element using –
element = driver.find_element(By.NAME, 'username')
Note – find_element_by_name() has been deprecated.
By.XPATH
With this strategy, the first element with pattern of xpath matching the location will be returned. If no element has a matching element attribute, a NoSuchElementException will be raised.
Syntax:
driver.find_element(By.XPATH, "xpath")
Example – For instance, consider this page source:
html
< html > < body > < form id="loginForm"> < input name="username" type="text" /> < input name="password" type="password" /> < input name="continue" type="submit" value="Login" /> </ form > </ body > < html > |
Now after you have created a driver, you can grab an element using:
login_form = driver.find_element(By.XPATH, "/html/body/form[1]") login_form = driver.find_element(By.XPATH, "//form[1]")
Note – find_element_by_xpath() has been deprecated.
By.LINK_TEXT
With this strategy, the first element with the link text value matching the location will be returned. If no element has a matching link text attribute, a NoSuchElementException will be raised.
Syntax:
driver.find_element(By.LINK_TEXT, "Text of Link")
Example: For instance, consider this page source:
html
< html > < body > < p >Are you sure you want to do this?</ p > < a href="continue.html">Continue</ a > < a href="cancel.html">Cancel</ a > </ body > < html > |
Now after you have created a driver, you can grab an element using
login_form = driver.find_element(By.LINK_TEXT, 'Continue')
Note – find_element_by_link_text() has been deprecated.
By.PARTIAL_LINK_TEXT
With this strategy, the first element with the partial link text value matching the location will be returned. If no element has a matching partial link text attribute, a NoSuchElementException will be raised.
Syntax:
driver.find_element(By.PARTIAL_LINK_TEXT, "Text of Link")
Example: For instance, consider this page source:
html
< html > < body > < p >Are you sure you want to do this?</ p > < a href="continue.html">Continue</ a > < a href="cancel.html">Cancel</ a > </ body > < html > |
Now after you have created a driver, you can grab an element using –
login_form = driver.find_element(By.PARTIAL_LINK_TEXT, 'Conti')
Note – find_element_by_partial_link_text() has been deprecated.
By.TAG_NAME
With this strategy, the first element with the given tag name will be returned. If no element has a matching tag name, a NoSuchElementException will be raised.
Syntax:
driver.find_element(By.TAG_NAME, "Tag name")
Example: For instance, consider this page source:
html
< html > < body > < h1 >Welcome</ h1 > < p >Site content goes here.</ p > </ body > < html > |
Now after you have created a driver, you can grab an element using –
login_form = driver.find_element(By.TAG_NAME, 'h1')
Note – find_element_by_tag_name() has been deprecated.
By.CLASS_NAME
With this strategy, the first element with the matching class attribute name will be returned. If no element has a matching class attribute name, a NoSuchElementException will be raised.
Syntax:
driver.find_element(By.CLASS_NAME, "class_of_element")
Example: For instance, consider this page source:
html
< html > < body > < p class="content">Site content goes here.</ p > </ body > < html > |
Now after you have created a driver, you can grab an element using –
content = driver.find_element(By.CLASS_NAME, 'content')
Note – find_element_by_class_name() has been deprecated.
By.CSS_SELECTOR
With this strategy, the first element with the matching CSS selector will be returned. If no element has a matching CSS selector, a NoSuchElementException will be raised.
Syntax:
driver.find_element(By.CSS_SELECTOR, "CSS Selectors")
Example: For instance, consider this page source:
html
< html > < body > < p class="content">Site content goes here.</ p > </ body > < html > |
Now after you have created a driver, you can grab an element using –
content = driver.find_element(By.CSS_SELECTOR, 'p.content')
Note: find_element_by_css_selector() has been deprecated.