0
next_btn = driver.find_element_by_xpath("//a[@class='ge-49M']")
for i in next_btn:
    driver.get(i)
    lnks = driver.find_elements_by_xpath("//a[@class='_2UzuFa']")
    for z in lnks:
        href_new.append(z.get_attribute('href'))

Here I am trying to retrieve all the links present in every page like(1,2,3,4,5) and from these links I want to retrieve the links of the products.

TypeError                                 Traceback (most recent call last)
<ipython-input-118-b4d6c76aecdf> in <module>
      1 next_btn = driver.find_element_by_xpath("//a[@class='ge-49M']")
----> 2 for i in next_btn:
      3     driver.get(i)
      4     lnks = driver.find_elements_by_xpath("//a[@class='_2UzuFa']")
      5     for z in lnks:

TypeError: 'WebElement' object is not iterable

But it's throwing this error.

0

Selenium has two functions for locating via xpath.

  • find_element_by_xpath which returns the first matching element or raises an exception if no matching elements are found.
  • find_elements_by_xpath (note the 's' on the end of elements) which returns a list containing every matching element; which will be empty in the case of no matching elements.

You're using the former function which returns a WebElement. WebElements aren't iterable so iterating over them is a TypeError. You should switch to the later function find_elements_by_xpath. This will return a list of elements you can iterate over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.