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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With