Locating single elements in Selenium Python


Selenium is a powerful automation tool that enables programmatic control of a web browser. It is crucial for automating web applications for various functions, including testing. Finding items on a webpage is an essential Selenium feature. In this article, we'll look at utilising Python to find specific Selenium bits.

Introduction to Selenium

A well-known open-source web-based automation tool is Selenium. Because of its adaptability, developers can create scripts using a variety of computer languages, including Python, Java, C#, and others. Selenium makes it possible to automate actions that you would ordinarily perform with a browser, like clicking, typing, choosing, and navigating.

Setting Up Selenium

Selenium needs to be set up correctly before we can start looking for components. Here is how to do it using pip:

pip install selenium

Selenium needs a browser-specific driver since it automates and regulates actions taken within a web browser. Download the ChromeDriver, for instance, from the Selenium website if you use Chrome.

Locating Elements in Selenium

The element that has to be engaged with while interacting with a webpage must be identified. Selenium provides a variety of methods for finding these items, including by ID, name, class name, link text, and other criteria. Selenium includes methods designed specifically for activities like information extraction and interaction with a single element.

Locating Single Elements

Selenium offers a number of ways to find a single element, such as −

  • 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

Example 1: Find_element_by_id

The first element with the relevant ID attribute is the result of this function. It raises a NoSuchElementException if there is no such element.

from selenium import webdriver

driver = webdriver.Chrome('path_to_chromedriver')
driver.get('https://www.website.com')

element = driver.find_element_by_id('element_id')
print(element.text)

Example 2: Find_element_by_name

With the given name, this function retrieves the first element. Due to the fact that form components frequently have the 'name' attribute, they are very helpful.

from selenium import webdriver

driver = webdriver.Chrome('path_to_chromedriver')
driver.get('https://www.website.com')

element = driver.find_element_by_name('element_name')
print(element.text)

Example 3: Find_element_by_xpath

A language called XPath was created for XML document navigation. It offers a method to choose items based on their characteristics, including ID, name, and more..

from selenium import webdriver

driver = webdriver.Chrome('path_to_chromedriver')
driver.get('https://www.website.com')

element = driver.find_element_by_xpath('//div[@class="class_name"]')
print(element.text)

Example 4: Find_element_by_link_text

The first link that exactly matches the link text is retrieved using this method. This is advantageous when working with anchor tags with the href attribute..

from selenium import webdriver

driver = webdriver.Chrome('path_to_chromedriver')
driver.get('https://www.website.com')

element = driver.find_element_by_link_text('Link Text')
print(element.text)

Example 5: Find_element_by_partial_link_text

The link that only partially matches the link text is returned by this method. When the link text is extensive and you can only recall a portion of it, this is useful.

from selenium import webdriver

driver = webdriver.Chrome('path_to_chromedriver')
driver.get('https://www.website.com')

element = driver.find_element_by_partial_link_text('Part of Link Text')
print(element.text)

Example 6: Find_element_by_tag_name

The first element with the specified tag name is obtained using this technique. When you need to access general tags like "body," "head," "title," etc., this can be useful.

from selenium import webdriver

driver = webdriver.Chrome('path_to_chromedriver')
driver.get('https://www.website.com')

element = driver.find_element_by_tag_name('tag_name')
print(element.text)

Example 7: Find_element_by_class_name

The first element with the appropriate class name is returned by this procedure. It's a useful technique to retrieve items that have a class name but no id or name properties.

from selenium import webdriver

driver = webdriver.Chrome('path_to_chromedriver')
driver.get('https://www.website.com')

element = driver.find_element_by_class_name('class_name')
print(element.text)

Example 8: Find_element_by_css_selector

The first element that matches the CSS selector is returned by this method. Being able to mix tag name, id, class name, and other properties makes it a flexible method of accessing components.

from selenium import webdriver

driver = webdriver.Chrome('path_to_chromedriver')
driver.get('https://www.website.com')

element = driver.find_element_by_css_selector('tag#id.class')
print(element.text)

Conclusion

In this article, we've examined the Python Selenium technique for finding single elements. Selenium is a really powerful tool that offers a tonne of versatility when working with web components. It makes it possible to automate almost any activity that can be done in a web browser, which is useful for things like testing web apps and scraping data from websites.

Use these tools responsibly and in accordance with the terms of service of the website you are visiting.

Updated on: 18-Jul-2023

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements