[Python] Type Error:'WebElement' object is not iterable What to do when an error occurs

environment

macOS Catalina 10.15.3 Python 3.6.5 selenium 3.141.0

Overview

Scraping Google search results with python selenium Extract the extracted elements to the list with a for loop

Issues that have occurred

'WebElement' object is not iterable Translation:'WebElement' object is not repeatable

Here, it was an error that occurred when performing a for loop, so the point is ‘Xxx’ specified as the reference source in for i in xxx cannot retrieve the repeating element. It is that.

Sample code

At this time, the pre-processing using seleniumu is omitted.

title_list = []
link_list = []
i = 1
i_max = 2

while i <= i_max:
    w = driver.find_element_by_class_name(“x”)
    for elem in x:
         title_list.append(elem.find_element_by_class_name(‘y’).text)           
         link_list.append(elem.find_element_by_tag_name(‘a’).get_attribute('href')) 

Error occurred in the above code

TypeError: 'WebElement' object is not iterable

Cause

#7th line here
w = driver.find_element_by_class_name(“x”)

.find_element extracts only a single element, so it's not repeatable.

Solution

# .find_elements… please
w = driver.find_elements_by_class_name(“x”)

You have to be careful about whether you need a single element extraction or multiple elements. I learned a lot.

reference

"I tried to extract the Google search title and URL list with Python" https://watlab-blog.com/2019/08/15/python-google-searchlist/

Recommended Posts

[Python] Type Error:'WebElement' object is not iterable What to do when an error occurs
[Python] What to do when an error related to SSL authentication is returned
[python] What to do when an error occurs in send_keys of headless chrome
What to do when an error occurs with import _ssl
[Beanstalk] What to do when an error occurs with import uuid
What to do when the value type is ambiguous in Python?
What to do if an error occurs when importing numpy with VScode
[OSX] [pyenv] What to do when an SSL error occurs in pip
[Python] What to do if an error occurs in pip (pyinstaller, pyautogui, etc.)
What to do if you get an error when installing python with pyenv
What to do if PyInstaller3.5 gives an error in Python3.8 (TypeError: an integer is required (got type bytes))
What to do when Japanese is not displayed on matplotlib
What to do when python3 type venv does not work well on Raspberry Pi
What to do if you get an OpenSSL error when installing Python 2 with pyenv
What to do when a Remove Error occurs when updating conda
What to do if you get an error when importing matplotlib in Python (Mac)
What to do if an error occurs when loading a python project created with poetry into VS Code
What to do when "Type Error: must be _socket.socket, not socket" appears on GAE
What to do when a Missing artifact occurs in a jar that is not defined in pom.xml
What to do when ModuleNotFoundError: No module named'XXX' occurs in Python
NameError: global name'dot_parser' is not defined and what to do when it comes up in python
What to do if pyenv install does not proceed with an error
What to do if you get an error when trying to load mnist
What to do if you get an error when installing Dlib (Ubuntu)
What to do when psycopg2 throws an error when pipenv lock under Pipenv environment
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
What to do when PermissionError of tempfile.mkstemp occurs
[Hyperas] Type Error: Resolving'function' object is not subscriptable
[Mac OS] What to do when Python is not installed as a framework. Is displayed when import matplotlib is performed.
How to write what to do when an application is first displayed in Qt for Python with Designer
[Django] What to do if an Integrity Error occurs when registering data from the management site to the database
[Python] I want to know the variables in the function when an error occurs!
What to do if CERTIFICATE_VERIFY_FAILED occurs when nltk.download () is done on macOS pyhon
What to do when SSL error occurs in pip in Windows10, miniconda, VScode environment
What to do if a Unicode Encode Error occurs in Sublime Text Python
What to do when "TypeError: data type not understood" appears in python's numpy.zeros
[EC2] What to do when selenium is stuck and processing does not proceed
What to do if an SSL connection error (ssl.SSLError: [SSL: DH_KEY_TOO_SMALL]) occurs on Ubuntu 20.04
curl: (60) What to do when Issuer certificate is invalid.
What to do when raise ValueError, "unsupported hash type"
What to do when "cannot import name xxx" [Python]
What to do if pyenv is not enabled (zsh)
What to do when you can't bind CaboCha to Python
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
Investigate the cause when an error is thrown when python3.8 is not found when using lambda-uploader with python3.8
What to do when is not in the sudoers file.This incident will be reported.
[Python] What to do when No module named'pyproj.datadir' appears when Exe is done with PyInstaller
What to do if the print command itself causes an error in Maya python
What to do if the progress bar is not displayed in tqdm of python
What to do if Python IntelliSense is not displayed in VS Code on Windows
What to do when Python starts up in Anaconda does not come out unexpectedly
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if you run python in IntelliJ and end with an error
What to do if you get the error "Error: opencv3: Does not support building both Python 2 and 3 wrappers" when installing openCV 3
What to do when PyCharm font is strange or garbled
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()
What to do when "SSL: CERTIFICATE_VERIFY_FAILED _ssl.c: 1056" appears in Python
What to do when you get an error saying "Name resolution temporarily failed" on linux
What to do when there is no response due to Proxy setting in Python web scraping
What to do if you get an error when running "certbot renew" in CakePHP environment
What to do when no display name occurs when unittesting Python + Tkinter on Github Actions Memo