error) 'WebElement' object is not iterable

ganadara·2022년 12월 18일
0

시행착오

목록 보기
3/8

코드

images = driver.find_element(By.CSS_SELECTOR,'.rg_i.Q4LuWd')
count = 1
for image in images:
    image.click()
    time.sleep(3)
    imgUrl = driver.find_element(By.CSS_SELECTOR,'.n3VNCb.KAlRDb').get_attribute("src")
    urllib.request.urlretrieve(imgUrl, str(count) + ".jpg")
    count = count + 1

오류

'WebElement' object is not iterable

원인

for image in images: 에서
images가 반복가능한 객체가 아니라 오류 발생
반복가능한 객체의 종류: 리스트, 딕셔너리, 문자열, range() 등등

해결

element -> elements

images = driver.find_elements(By.CSS_SELECTOR,'.rg_i.Q4LuWd')
profile
DL 공부중

1개의 댓글

comment-user-thumbnail
2023년 4월 4일

🧸

답글 달기