Home > Mobile >  Selenium: 'list' object has no attribute 'find_elements'
Selenium: 'list' object has no attribute 'find_elements'

Time:02-02

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'

Can anyone help me explain why it behaves this way? And what can I do to grab that element by its class name?

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

CodePudding user response:

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