TypeError: ‘WebElement’ object is not iterable

Hello coders! Today, we’re addressing an error that you may encounter when working with Python’s Selenium webdriver: “TypeError: ‘WebElement’ object is not iterable.”

What is the “TypeError: ‘WebElement’ object is not iterable” error?

This TypeError arises when you are trying to iterate through a ‘WebElement’ object, which is not iterable.

In Selenium, we have the find_element_by_xpath method and find_elements_by_xpath method. When you use the find_element_by_xpath, it returns only a single WebElement. Since the WebElement is not iterable, you will encounter a TypeError when you try to loop over it.

On the other hand, find_elements_by_xpath returns a list of WebElement(s). As lists are iterable in Python, you will not encounter this error when you use the latter method.

What causes the “TypeError: ‘WebElement’ object is not iterable” error?

The primary reason behind this TypeError is the use of the find_element_by_xpath method when trying to loop over multiple WebElements. Since find_element_by_xpath only returns one WebElement, Python will throw a TypeError when you try to iterate over it.

Let’s look at this with some examples.

Code Examples

Let’s take an incorrect code example that causes the TypeError:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.google.co.in/")
links = driver.find_element_by_tag_name('a')

for i in links:
    print(i.get_attribute('href'))  

Running this code will give you the TypeError: ‘WebElement’ object is not iterable, because the find_element_by_tag_name method only returns one WebElement.

How to resolve the “TypeError: ‘WebElement’ object is not iterable” error?

The key to resolving this error is to replace find_element_by_xpath (or find_element_by_tag_name, in the example) with find_elements_by_xpath.

Here is a correct version of the code:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.google.co.in/")
links = driver.find_elements_by_tag_name('a')

for i in links:
    print(i.get_attribute('href'))  

In this example, find_elements_by_tag_name is used, and it will return a list of WebElement(s). You can loop over the list and print the ‘href’ attribute of each WebElement.

FAQs

1. Can we make WebElement iterable?

No, a WebElement is not iterable in Python, but a list of WebElement(s) is iterable.

2. What is the difference between find_element_by_xpath and find_elements_by_xpath in Selenium?

The find_element_by_xpath method returns the first matching WebElement, whereas find_elements_by_xpath returns a list of all matching WebElement(s).

3. What other methods in Selenium return a list of WebElement(s)?

Other than find_elements_by_xpath, there are find_elements_by_class_name, find_elements_by_css_selector, find_elements_by_id, find_elements_by_link_text, find_elements_by_partial_link_text, find_elements_by_name, and find_elements_by_tag_name.

Conclusion

In conclusion, the “TypeError: ‘WebElement’ object is not iterable” is a common error in Selenium that arises when you attempt to iterate through a ‘WebElement’ object returned by methods like find_element_by_xpath or find_element_by_tag_name. This error can be resolved by using methods that return a list of WebElement(s) such as find_elements_by_xpath or find_elements_by_tag_name.

These return a list of matching WebElement(s), which you can then iterate over without raising an error. Always remember, a single WebElement in Python is not iterable, however, a list of WebElement(s) is.

So, ensure that you’re retrieving multiple elements when you need to iterate over web elements in Selenium.

References

  1. Locating Elements in Selenium

Follow us at PythonClear to learn more about solutions to general errors one may encounter while programming in Python.

Leave a Comment