简体   繁体   English

Selenium: 'list' object 没有属性 'find_elements'

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

I saw this answer but couldn't figure out why it behaves this way.我看到了这个答案,但无法弄清楚为什么它会这样。 So I have the following code:所以我有以下代码:

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

And I get an error on the last line ' list' object has no attribute 'find_elements '我在最后一行 ' list' object has no attribute 'find_elements '

Can anyone help me explain why it behaves this way?谁能帮我解释为什么它会这样? And what can I do to grab that element by its class name?我该怎么做才能通过名称 class 获取该元素?

EDIT: Complete error:编辑:完整错误:

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")>

Process finished with exit code 1进程结束,退出代码为 1

The code you posted doesn't match the code in the error.您发布的代码与错误中的代码不匹配。 Put this line in your code and it should work fine.将此行放入您的代码中,它应该可以正常工作。

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