Testing Taxi

Intelligent Assistant for Testers


Selenium | How to find Elements in seconds

Are you a software tester struggling with finding elements quickly in Selenium? Look no further! In this article, we will guide you through the process of finding elements in just 2 seconds using Selenium. Say goodbye to hours of endless searching and say hello to efficient and effective testing! Let’s dive right in and discover the secrets to speeding up your element finding skills.

Table of Contents

How to find an element by class

To find an element by class using Selenium in Python, you can use the find_elements method. This method takes in a string parameter representing the class name of the element you want to find. This technique is useful when you want to locate an element based on its class attribute, which is commonly used in web development to apply styling or group similar elements together. For testers, finding an element by class can be beneficial when you want to interact with multiple elements that share the same class, such as verifying the presence or absence of certain elements or performing actions on them.

Here’s an example of counting the number of nav links on a page.

driver = webdriver.Firefox()

# navigate to a page

# get the links on the page
links = driver.find_elements(By.CSS_SELECTOR, ".nav-link")
assert len(links) == 5

How to find an element by XPath

XPath is a powerful way that software testers can locate elements on a web page. XPath is sort of like an element ID, except it can be used to navigate through elements in an HTML document even if ids or class names are missing. In Selenium, the find_element method can be used to find elements using XPath expressions.

To find an element by XPath, first identify the XPath expression for the element you want to locate. This expression acts as a pathway to the element within the HTML structure. Then, use the find_element method and provide the By.XPATH locator strategy along with the XPath expression as a parameter. This will return the first matching element found on the page.

Using XPath to find elements can be especially helpful for testers in certain situations. For example, when elements do not have a unique identifier such as an ID or class, XPath can be used to locate them based on their position in the HTML structure. XPath can also be used to find elements based on their text content or any other attribute values. This flexibility allows testers to target specific elements accurately, even if their attributes change dynamically. Ultimately, XPath provides testers with a versatile and reliable way to find elements on a web page during automated testing.

Here’s an example of checking if logo without any class or id is present

driver = webdriver.Firefox()

# navigate to a page

# get the links on the page
logo = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div/img")
assert logo.size[“width”] == 150
Finding XPath in Selenium Example

How to find an element by text content

It’s a bit tricky to find elements by text content in Selenium because there aren’t any simple methods for this purpose. But, we can do it via the find_elements(By.XPATH) method using the contains syntax. This way we can search for elements that contain a specific text string. This is helpful when the element is dynamic and its class name or id may change frequently. By searching for the element based on its text content, testers can ensure that their automated tests are reliable and accurate.

Here’s the XPath syntax “//*[contains(text(),'<search-content>’)]”

Here’s an example in Python finding specific links on Stackoverflow

driver = webdriver.Firefox()

# navigate to a page

# get the “add a comment” links
addCommentLinks = driver.find_elements(By.XPATH, ‘//*[contains(text(),\'Add a comment\')]’)
assert len(addCommentLinks) == 5
Finding Add a Comment for Selenium example

How to find an element by id

To find an element by its id in Selenium, we can use the `find_element` method. This method searches for an element based on its id attribute. It is useful to use this method when we know the unique id value of the element we want to locate. By specifying the id, we can directly target the desired element without the need to analyze its class or other attributes. This method is particularly helpful for testers because it allows them to quickly and accurately locate specific elements on a web page, making it easier to perform automated tests and verify the expected functionality.

Here’s an example with Python finding an element by ID on MDN

driver = webdriver.Firefox()

# navigate to a page

# get the logo
logo = driver.find_elements(By.ID, ‘mdn-docs-logo’)
assert logo.size[“height”] == 83
Finding element by ID in selenium example

How to find an element by name

To find an element by its name attribute using Selenium in Python, we can use the find_element method with the argument “name” followed by the name attribute value we are looking for.

The name attribute is used to define a name for an element in HTML. Sometimes, elements on a webpage may not have a unique id or class name, but they may have a unique name attribute. This often happens in input forms. In such cases, it makes sense to use the name attribute to locate elements.

This method is helpful for testers because it allows them to specifically target elements based on their unique name attribute. By using this method, testers can ensure that they interact with the correct element on a webpage without relying on other attributes that may not be as reliable. This helps testers to accurately automate tests and validate the functionality of the application.

Here’s an example of finding the username on a login form

driver = webdriver.Firefox()

# navigate to a page

# get the username field and type an email in it
usernameInput = driver.find_element(By.Name, ‘username)
Find input by name in Selenium example

Putting it all together

In conclusion, Selenium provides various methods to easily find elements in web pages. By using these techniques, software testers can efficiently locate specific elements such as classes, XPaths, text content, IDs, and names. Mastering the art of element finding in Selenium will greatly enhance the effectiveness and productivity of testing automation. With the ability to accurately and swiftly identify elements, testers can confidently navigate and interact with web applications, ensuring the delivery of high-quality software. So, start exploring Selenium’s powerful element finding capabilities and take your testing automation to the next level.


Share the Post:

Related Posts

Notes Feature

Taxi Notes let you record contextual notes while you’re testing. Jot a note while you’re in the user’s section and

Read More