Turn your manual testers into automation experts! Request a DemoStart testRigor Free

UnexpectedTagNameException in Selenium: Causes and Remedies

The tag name is a property used in Selenium to retrieve the tag name of a web element. Knowing the tag name helps validate or check the element type before performing actions on it.

UnexpectedTagNameException occurs in Selenium when scripts perform an action on the web element with an inappropriate tag name. This error is specific to the scenarios when WebDriver expects a particular tag name and finds another one.

Causes for UnexpectedTagNameException in Selenium

Below are the most common causes for UnexpectedTagNameException in Selenium:

Inappropriate Usage with Select Class

The Select class in Selenium provides methods to interact with dropdown menus.

These dropdown menus are expected to be defined using the <select> HTML tag.

Methods like select_by_value(), select_by_index(), or select_by_visible_text() are intended to be used with <select> elements. If these methods are used on a different type of element, like <div>, <span>, or <input>, an UnexpectedTagNameException will be thrown.

Below is an example of using the ‘Button’ element in the Select class:
from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome()
driver.get("http://example.com")
button_element = driver.find_element_by_id("button-id") # Assuming this is a <button> element

try:
  select = Select(button_element)

except UnexpectedTagNameException:
  print("Caught an UnexpectedTagNameException!")

Dynamic Web Content

Sometimes, the structure of a web page might change due to the presence of dynamic content. The script is written considering a different tag and then encounters another tag during execution, causing the exception to be thrown.

Incorrect Locators

If you use incorrect locators in the script that select an element different from what is intended, the UnexpectedTagNameException will be thrown.

Remedies for UnexpectedTagNameException in Selenium

Below are the steps you can take to mitigate UnexpectedTagNameException:

Make Correct Element Selection

Make sure that you are selecting the correct element. Use developer tools in your browser to inspect the web element and then confirm its tag. Use the proper locator strategy (like XPath, CSS Selector, ID, etc.) to know and target the desired <select> element correctly.
from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome()
driver.get("http://example.com")

# Incorrect selection - may cause UnexpectedTagNameException
# element = driver.find_element_by_id("non-select-element")
# Correct selection

element = driver.find_element_by_id("select-element-id")
select = Select(element) # Should not raise an exception if element is a <select>

Verify Tag Name

Before you code operations with specific tags (e.g., using the Select class) in the script, it is advisable to verify the tag name of the web element using the tag_name property. If the tag_name does not have select, then you should not use any Select class method.
from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome()
driver.get("http://example.com")
element = driver.find_element_by_id("element-id")

if element.tag_name == "select":
  select = Select(element)
else:
  print(f"Expected a <select> element, but got <{element.tag_name}>")

Perform Exception Handling

Code try-except (in Python) and try-catch (in Java) blocks to handle UnexpectedTagNameException gracefully. Handling the exception will help your script fail gracefully with an informative error message or continue the test run smoothly.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import UnexpectedTagNameException

driver = webdriver.Chrome()
driver.get("http://example.com")

try:
  element = driver.find_element_by_id("element-id")
  select = Select(element)

except UnexpectedTagNameException:
  print("Element is not a <select> tag, cannot use Select class methods.")

Use testRigor to Bypass Exceptions Forever

Selenium has been a popular test automation tool for a long time. However, today, it might act more as a roadblock in your testing journey. With the plethora of errors and exceptions to handle during script writing, Selenium does not match the Agile and DevOps methodologies of fast releases and short cycles.

Alternatives available can quickly walk in tandem with your CI/CD pipelines, and you can bid goodbye to the irritating exceptions forever. testRigor is a codeless, generative AI-based test automation tool that lets you create test cases in plain English. It doesn’t use unstable XPath and CSS locators; it is based on more humanized element identifiers known as testRigor locators.

In plain English, write the UI text you see on the screen as element identifiers. Tag names or IDs, etc., are not used as element identifiers. As a human, you can rely on a human’s way of referring to elements like sections, relative locations, tables, etc.

See an example of test steps below:
click on button "Delete" below "Section Name" to the right of "label"
check that checkbox "Keep me signed in" is checked
click on table "actions" at row "103" and column "Action"

You can see how easy it is to interact with buttons, checkboxes, and tables using testRigor. It is just plain English, and you are done! testRigor’s advanced features help efficiently execute your web, mobile, desktop, and API tests singlehandedly. It seamlessly integrates with the major test management, CI/CD, and infrastructure providers to expand your testing horizons.

Here is the list of the top testRigor’s features.

Conclusion

If you feel exhausted from troubleshooting and maintaining your Selenium scripts, it is time to shift. Use the power of NLP and AI to fasten and comprehensively improve your test automation activities. With minimum effort and time, achieve more expansive test coverage without getting trapped into errors and exceptions.

Use your precious time to build more robust tests with testRigor than in debugging and exception handling the Selenium scripts.

Join the next wave of functional testing now.
A testRigor specialist will walk you through our platform with a custom demo.
Related Articles

SAP Concur Testing

Concur Technologies, now SAP Concur, is an American software-as-a-service (SaaS) company providing travel and expense management ...

Authorize.net Testing

You need a payment gateway in several scenarios, especially when you are running a business that requires secure, efficient, and ...

KYC Portal CLM Testing

KYC Portal CLM is an award-winning Client Lifecycle Management (CLM) platform designed to streamline and automate the Know Your ...