簡體   English   中英

Selenium: 'list' object 沒有屬性 'find_elements'

[英]Selenium: 'list' object has no attribute 'find_elements'

我看到了這個答案,但無法弄清楚為什么它會這樣。 所以我有以下代碼:

import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://www.linkedin.com/jobs/search?position=1&pageNum=0")
time.sleep(1)
# user_name = "Product Designer"


inputElement = driver.find_element("xpath", '/html/body/div[1]/header/nav/section/section[2]/form/section[1]/input')
inputElement.send_keys('Product Designer at Apple')

inputElement.send_keys(Keys.ENTER)

time.sleep(1)


jobs_block = driver.find_elements(By.CLASS_NAME, "jobs-search__results-list")
print(jobs_block[0])
jobs_list = jobs_block.find_elements(By.CLASS_NAME, ".base-card")
links = []

for job in jobs_list:
all_links = job.find_elements_by_tag_name('a')
for a in all_links:
    if    str(a.get_attribute('href')).startswith("https://www.linkedin.com/jobs/view") and a.get_attribute('href') not in links:
        links.append(a.get_attribute('href'))
    else:
        pass

我在最后一行 ' list' object has no attribute 'find_elements '

誰能幫我解釋為什么它會這樣? 我該怎么做才能通過名稱 class 獲取該元素?

編輯:完整錯誤:

Traceback (most recent call last):
File "/Users/me/project/main.py", line 23, in <module>
jobs_list = jobs_block.find_elements(By.CLASS_NAME, ".base-card")
AttributeError: 'list' object has no attribute 'find_elements'
<selenium.webdriver.remote.webelement.WebElement (session="68539ee5ad7d0468041a68944c5070ce", element="0a813269-84e0-4331-b220-a21973c39aa1")>

進程結束,退出代碼為 1

您發布的代碼與錯誤中的代碼不匹配。 將此行放入您的代碼中,它應該可以正常工作。

jobs_list = jobs_block[0].find_elements(By.CLASS_NAME, "base-card")

暫無
暫無

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

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