Locating elements in selenium using python

mahesh reddy
6 min readMay 14, 2021

When you want to carry out some automatic operation on a web page you will need the locators. These are unique web element identifiers, such as text, buttons, tables, div, etc.

Interacting with the web page is not possible if the test script cannot find Web elements. Selenium Web driver provides the following web element localization techniques.

Locate Objects by Name using Python

Defining unique ids for Web elements in an HTML code is a standard practice. There may be occasions, however, when those unique identifiers are not available. Alternatively, there are the names; you can then also use them to pick a web element.

Here is the code snippet showing the use of the method < find element by name >. The code below opens the window to Google and performs a search for text.

Web driver manufactured from selenium

Import Keys from selenium.webdriver.common.keys

Driver = web. Firefox()

Driver.get(“google.com)

Driver.window maximize()

Zeit.sleep(5)

ElementInput = driver.find element by name(“q)

Item input.send keys(“techbeamers()

Element.submit()

Zeit.sleep(20)

Controller.close()

If the HTML code has more than one web element with an attribute “@name,” then the first web element will be selected from the list by this method. If there is no match a NoSuchElementException is issued.

Locate Identified Elements

You use this method when there is an I d attribute available for the element. Indeed, it is the most accurate and quickest way to locate a particular web item on an HTML page. An Id for any object on a web page will always be unique. So, you would choose to use the I d attribute to locate the elements over other options available.

If You are interested to Learn Python You can enroll for free live demo Python Online Training

Here is the code snippet demonstrating how the < find element by id > method is used. The code below opens the window to Google and performs a search for text.

Import Keys from selenium.webdriver.common.keys

Driver = web. Firefox)

Driver.get(“google.com)

Driver.window maximize()

Zeit.sleep(5)

ElementInput = driver.find element by id(“lst-ib)

Item input.send keys(“techbeamers)

Element.submit()

Zeit.sleep(20)

Controller.close()

If more than one web element has the same I d value, attribute, then this method returns the first element the I d matches with. When there is no match it will lift a NoSuchElementException.

Locate Elements by Text Link using Python

This method is used to pick hyperlinks from a Web page. If multiple elements have the same link text, then this method will select a match for the first element. It only operates on links (hyperlinks), and so you call it < Link Text locator >.

Here is the code snippet illustrating the use of the method < find element by link text >. The code below opens the window to Google and performs a search for text.

Import Keys from selenium.webdriver.common.keys

Driver = web. Firefox)

Driver.get(“google.com)

Driver.window maximize)

Zeit.sleep(5)

ElementInput = driver.find element by name(“q)

Item input.send keys(“techbeamers)

Element.submit)

Zeit.sleep(5)

elem = driver.find element by link text(“Tutorial for Pythons)

On.click)

Zeit.sleep(20)

Controller.close)

Locate Elements through Part Link Text using Python

You need to provide the complete Link text to locate the item by using the link text process. The partial link text method, however, allows us to select a hyperlink by giving only a fraction of the link text.

If you use the partial link text method in the above example, the code will then become as.

Import Keys from selenium.webdriver.common.keys

Driver = web. Firefox()

Driver.get(“google.com()

Driver.window maximize()

Zeit.sleep(5)

ElementInput = driver.find element by name(“q)

Item input.send keys(“techbeamers)

Element.submit)

Zeit.sleep(5)

Find element by partial link text(“Python)

On.click)

Zeit.sleep(20)

Controller.close()

This code opens the web-page of the Python tutorial as in the code above.

Locate by Xpath Elements

  • Another useful way to find an item is using the selenium find element XPath by python. You use XPath to access the element when a proper I d or name attribute is not present in the code.
  • XPath lets you locate an item using either the Absolute (not the preferred way), or the Relative XPath. Absolute XPaths specifies the root position of an object (html). Using Absolute XPath isn’t an efficient tool, however
  • This is because if you just make a small improvement in the code of the web page. Absolute XPath can shift, and it may not be possible for the webdriver to locate the item with the old.
  • In the case of Relative XPath, you try to locate a nearby element (ideally a parent element) for which an I d or name attribute is given. Now you can measure the target element’s XPath relative to the nearby object. This XPath ‘s chances of changing are very low , making our tests more reliable.
  • And both of these ways enable us to locate an item that has an attribute I d or name.
  • XPath locators may also use other attributes than I d and name to locate an entity.
  • For user SignUp, let’s take the following HTML code to understand the Absolute and Relative direction.

< html > < html

< Corps >

< Form id=”Form Signup “>

< name input=’emailId / mobileNo ‘type=’text’/ >

Form < input name=”password “/ >

< name for input=”continue “type=”submit” value=”SignUp”/ >

< name for input=”continue “type=”button” value=”Clear”/ >

< /form > < /form >

< /Corps >

< html > < html

Now you are going to try using XPath to find different elements present on the list.

Here are the XPaths which help the Selenium Webdriver locate the element of the form.

It is the Road to the Absolute. If you make some improvement to the HTML code it will crash. Now the Relative Xpaths follow.

Driver.find element by xpath(“/form[1])

It marks the dimension of first form.

Driver.find element by xpath(“/form[@id=’signUpForm])

To locate the element, it uses the id attribute with value as the “signUpForm.”

Similarly you can locate the item < emailId / mobileNo >.

Driver.find element by xpath(“/form[input/@name=’emailId / mobileNo])

It returns the first element of the form which has the child element ‘data.’ This input element has a ‘name’ attribute and a ‘emailId/mobileNo’ value.

Email input = driver.find element by xpath(‘/form[@id=’loginForm’]/input[1])

It selects the first child element of the ‘form’ element to ‘file.’ Where the item of the form has the ‘id’ attribute and the ‘signUpForm’ value.

Driver.find element by xpath(“/input[@name=’emailId / mobileNo])”

It goes directly to the element ‘data’ with a value attribute name as ‘emailId/mobileNo.’

Locate par CSS selector elements

This method lets you find elements by name of the class attribute.

The first element matching the input attribute will be returned. If the check fails the NoSuchElementException method will be thrown.

For example , assume the HTML code below:

< html > < html

< Corps >

Click Here</p > < div class=”round-button “>

< /Corps >

< html > < html

The code above has a single div item of the class form “round-button.” You may use the symbol dot.) (to access a CSS class. The syntax below for the “round-button” class reflects the CSS selector.

Rund-button Div.

You can locate the target div element according to the CSS locator strategy using the code below.

Find element by css selector(‘div.round-button’)

Locate by Tagname Elements using Python

This method lets you find a web-element by specifying the name of the tag.

It returns the first item which has the name defined. If the search is not successful, NoSuchElementException will be thrown at the process.

For example , assume the HTML code below.

< html > < html

< Corps >

< heading > Hello Python</title >

< p > Learn automated testing using Python</p >

< /Corps >

< html > < html

The code above has a title tag which contains some text. You will find them using the code below.

Get div = driver.find element(‘title)

Locate par Classname elements using Python

This method lets you locate elements based on the name of the class.

The first item with the given class name will be returned. If the search is not successful, NoSuchElementException will be thrown at the process.

For example , assume the HTML code below.

< html > < html

< Corps >

Tap Here</div > < div class=”round-button “>

< /Corps >

< html > < html

The code above contains a class with a name. You will find them using the code below.

Get div = round-button driver.find element by class name()

Fast wrap up-Using Selenium Python to locate elements. You hope you now know how to use the locators and use them to locate objects.

Conclusion:

I hope you reach a conclusion about selenium and python. You can learn more through Python online training.

--

--

mahesh reddy

Python certification training course will help you master the concepts and gain in-depth experience on writing Python code and packages like SciPy, Matplotlib,