本文介紹了TypeError:“WebElement"對象不是可迭代的錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試從維基百科主頁中提取所有鏈接,但此代碼顯示 TypeError: 'WebElement' object is not iterable error.
I am trying to extract all the links from wikipedia homepage but this code showing TypeError: 'WebElement' object is not iterable error.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser=webdriver.Chrome()
browser.get('https://en.wikipedia.org/wiki/Main_Page')
search=[]
search=browser.find_element_by_xpath('//*[@href]')
for ii in search:
print(ii.get_attribute('href'))
time.sleep(4)
browser.close()
推薦答案
問題是你使用的 find_element_by_xpath
只返回一個 WebElement(不可迭代),find_elements_by_xpath
返回一個 WebElement 列表.
The problem is that you are using find_element_by_xpath
which return only one WebElement (which is not iterable), the find_elements_by_xpath
return a list of WebElements.
解決方法:將 find_element_by_xpath
替換為 find_elements_by_xpath
Solution: replace find_element_by_xpath
with find_elements_by_xpath
參考:selenium-python 文檔
這篇關于TypeError:“WebElement"對象不是可迭代的錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!