Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium / Python TypeError: 'WebElement' object is not iterable

I'm trying to print and/or write to file text inside a span tag from the following HTML. Only want it to find_element once, not find_elements since there's only one instance:

<div>
  <span class="test">2</span>
</div>

Below is the python code I'm using that is generating the "'WebElement' object is not iterable" error.

test = driver.find_element_by_xpath("/html/body/div")

for numberText in test:
numberTexts = numberText.find_element_by_class_name("test")

print(numberTexts.txt)
like image 552
Bronson77 Avatar asked Oct 30 '25 06:10

Bronson77


1 Answers

You're getting a single element (first one) by:

driver.find_element_by_xpath("/html/body/div")

which is obviously not iterable.

For multiple elements i.e. to get an iterable, use:

driver.find_elements_by_xpath("/html/body/div")

Note the s after element.

Also check out the documentation.

like image 199
heemayl Avatar answered Oct 31 '25 19:10

heemayl