我的代碼需要幫助。我在 python 中使用 Selenium
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)
The error is "TypeError: 'WebElement' object is not iterable".
我知道 elems 的結果是一個 WebElement,但我需要使用 find_element_by_xpath 因為我想要一個特定的 div 而這個 div 沒有唯一的 id。我沒有找到在(例如)字串中轉換 WebElement 的方法。您是否知道另一種擁有此特定 div 但不使用 find_element_by_xpath 的方法?你有什么其他的想法來解決這個問題嗎?
uj5u.com熱心網友回復:
for job in elems:
如果你注意,編譯器會認為 elems 是一個串列。
但你已經定義elems為
elems = driver.find_element_by_xpath("//article[@class='page-container']/section/div[3]")
這將回傳WebElement不是串列WebElement。
問題說明:
請注意,find_element_by_xpath回傳一個 web 元素,而 asfind_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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329943.html
上一篇:最小化硒
下一篇:硒中沒有xpath點擊
