簡體   English   中英

堆棧溢出“TypeError:'WebElement' 對象不可迭代”

[英]Stack overflow "TypeError: 'WebElement' object is not iterable"

我的代碼需要幫助。 有關信息,我將 Selenium 與 python 一起使用

elems = driver.find_element_by_xpath("//article[@class='page-container']/section/div[3]") 

for job in elems:
    job_list.append(job.get_attribute('href')) 
print(job_list)

錯誤是

"TypeError: 'WebElement' object is not iterable".

我知道 elems 的結果是一個 WebElement,但我需要使用 find_element_by_xpath 因為我想要一個特定的 div 而這個 div 沒有唯一的 id。 我沒有找到在(例如)字符串中轉換 WebElement 的方法。
您是否知道另一種擁有此特定 div 但不使用 find_element_by_xpath 的方法?
你有什么其他的想法來解決這個問題嗎?

for job in elems:

如果你注意,編譯器會認為 elems 是一個列表。

但是你已經將elems定義為

elems = driver.find_element_by_xpath("//article[@class='page-container']/section/div[3]") 

這將返回一個WebElementWebElement的名單。

問題說明:

請注意, find_element_by_xpath返回一個 web 元素,而find_elements_by_xpath返回一個列表。

修復:(使用find_elements而不是find_element

elems = driver.find_elements_by_xpath("//article[@class='page-container']/section/div[3]") 

for job in elems:
    job_list.append(job.get_attribute('href')) 
print(job_list)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM