4.Locating Elements

There are various strategies to locate elements in a page. You can use the mostappropriate one for your case. Selenium provides the following method tolocate elements in a page:

  • find_element

To find multiple elements (these methods will return a list):

  • find_elements

Example usage:

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

The attributes available for theBy class are used to locate elements on a page.These are the attributes available forBy class:

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

The ‘By’ class is used to specify which attribute is used to locate elements on a page.These are the various ways the attributes are used to locate elements on a page:

find_element(By.ID,"id")find_element(By.NAME,"name")find_element(By.XPATH,"xpath")find_element(By.LINK_TEXT,"link text")find_element(By.PARTIAL_LINK_TEXT,"partial link text")find_element(By.TAG_NAME,"tag name")find_element(By.CLASS_NAME,"class name")find_element(By.CSS_SELECTOR,"css selector")

If you want to locate several elements with the same attribute replace find_element with find_elements.

4.1.Locating by Id

Use this when you know theid attribute of an element. With this strategy,the first element with a matchingid attribute will be returned. If noelement has a matchingid attribute, aNoSuchElementException will beraised.

For instance, consider this page source:

<html><body><formid="loginForm"><inputname="username"type="text"/><inputname="password"type="password"/><inputname="continue"type="submit"value="Login"/></form></body></html>

The form element can be located like this:

login_form=driver.find_element(By.ID,'loginForm')

4.2.Locating by Name

Use this when you know thename attribute of an element. With this strategy,the first element with a matchingname attribute will be returned. If noelement has a matchingname attribute, aNoSuchElementException will beraised.

For instance, consider this page source:

<html><body><formid="loginForm"><inputname="username"type="text"/><inputname="password"type="password"/><inputname="continue"type="submit"value="Login"/><inputname="continue"type="button"value="Clear"/></form></body></html>

The username & password elements can be located like this:

username=driver.find_element(By.NAME,'username')password=driver.find_element(By.NAME,'password')

This will give the “Login” button as it occurs before the “Clear” button:

continue_button=driver.find_element(By.NAME,'continue')

4.3.Locating by XPath

XPath is the language used for locating nodes in an XML document. As HTML canbe an implementation of XML (XHTML), Selenium users can leverage this powerfullanguage to target elements in their web applications. XPath supports thesimple methods of locating by id or name attributes and extends them by openingup all sorts of new possibilities such as locating the third checkbox on thepage.

One of the main reasons for using XPath is when you don’t have a suitable id orname attribute for the element you wish to locate. You can use XPath to eitherlocate the element in absolute terms (not advised), or relative to an elementthat does have an id or name attribute. XPath locators can also be used tospecify elements via attributes other than id and name.

Absolute XPaths contain the location of all elements from the root (html) and asa result are likely to fail with only the slightest adjustment to theapplication. By finding a nearby element with an id or name attribute (ideallya parent element) you can locate your target element based on the relationship.This is much less likely to change and can make your tests more robust.

For instance, consider this page source:

<html><body><formid="loginForm"><inputname="username"type="text"/><inputname="password"type="password"/><inputname="continue"type="submit"value="Login"/><inputname="continue"type="button"value="Clear"/></form></body></html>

The form elements can be located like this:

login_form=driver.find_element(By.XPATH,"/html/body/form[1]")login_form=driver.find_element(By.XPATH,"//form[1]")login_form=driver.find_element(By.XPATH,"//form[@id='loginForm']")
  1. Absolute path (would break if the HTML was changed only slightly)

  2. First form element in the HTML

  3. The form element with attributeid set tologinForm

The username element can be located like this:

username=driver.find_element(By.XPATH,"//form[input/@name='username']")username=driver.find_element(By.XPATH,"//form[@id='loginForm']/input[1]")username=driver.find_element(By.XPATH,"//input[@name='username']")
  1. First form element with an input child element withname set tousername

  2. First input child element of the form element with attributeid set tologinForm

  3. First input element with attributename set tousername

The “Clear” button element can be located like this:

clear_button=driver.find_element(By.XPATH,"//input[@name='continue'][@type='button']")clear_button=driver.find_element(By.XPATH,"//form[@id='loginForm']/input[4]")
  1. Input with attributename set tocontinue and attributetype set tobutton

  2. Fourth input child element of the form element with attributeid set tologinForm

These examples cover some basics, but in order to learn more, the followingreferences are recommended:

Here is a couple of very useful Add-ons that can assist in discovering the XPathof an element:

4.4.Locating Hyperlinks by Link Text

Use this when you know the link text used within an anchor tag. With thisstrategy, the first element with the link text matching the provided value willbe returned. If no element has a matching link text attribute, aNoSuchElementException will be raised.

For instance, consider this page source:

<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>

The continue.html link can be located like this:

continue_link=driver.find_element(By.LINK_TEXT,'Continue')continue_link=driver.find_element(By.PARTIAL_LINK_TEXT,'Conti')

4.5.Locating Elements by Tag Name

Use this when you want to locate an element by tag name. With this strategy,the first element with the given tag name will be returned. If no element has amatching tag name, aNoSuchElementException will be raised.

For instance, consider this page source:

<html><body><h1>Welcome</h1><p>Sitecontentgoeshere.</p></body></html>

The heading (h1) element can be located like this:

heading1=driver.find_element(By.TAG_NAME,'h1')

4.6.Locating Elements by Class Name

Use this when you want to locate an element by class name. With this strategy,the first element with the matching class name attribute will be returned. Ifno element has a matching class name attribute, aNoSuchElementExceptionwill be raised.

For instance, consider this page source:

<html><body><pclass="content">Sitecontentgoeshere.</p></body></html>

The “p” element can be located like this:

content=driver.find_element(By.CLASS_NAME,'content')

4.7.Locating Elements by CSS Selectors

Use this when you want to locate an element usingCSS selectorsyntax. With this strategy, the first element matching the given CSS selectorwill be returned. If no element matches the provided CSS selector, aNoSuchElementException will be raised.

For instance, consider this page source:

<html><body><pclass="content">Sitecontentgoeshere.</p></body></html>

The “p” element can be located like this:

content=driver.find_element(By.CSS_SELECTOR,'p.content')

Sauce Labs has good documentation on CSSselectors.