0

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

7
  • Have you tried printing jobs_block to see what it is? Or jobs_block[0]?
    – Axe319
    Feb 1 at 14:57
  • @Axe319 I did. I get <selenium.webdriver.remote.webelement.WebElement (session="c8e9e46d0bbb23e4d67811d7a8f25116", element="dafbac11-1901-4a7f-b4ea-cd263f39f62b")> which I'm not sure why isn't resembling a list. So the problem is probably lying in how I'm using web driver?
    – Mansidak
    Feb 1 at 15:00
  • What is the full exception that you received? Can you include it in the question?
    – Axe319
    Feb 1 at 15:02
  • 1
    What you have posted looks fine. Are you sure the error is on one of the lines you've posted? Edit your question and post the full error message (as text) and indicate which line in your code the error is being triggered.
    – JeffC
    Feb 1 at 15:02
  • @Axe319 I added the complete error in the question as an edit
    – Mansidak
    Feb 1 at 15:05

1 Answer 1

0

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")
5
  • I tried that and I'm getting this error now: all_links = job.find_elements_by_tag_name('a') AttributeError: 'WebElement' object has no attribute 'find_elements_by_tag_name'
    – Mansidak
    Feb 1 at 15:07
  • 1
    Yep. That means that the error you posted is fixed which means this question is answered. The new issue should be a new question if you need help. Having said that... use find_element(By.TAG_NAME, "tag name") and that issue should be fixed as well.
    – JeffC
    Feb 1 at 15:09
  • Here's a link to the docs that lists all the find_element() By types, selenium-python.readthedocs.io/locating-elements.html
    – JeffC
    Feb 1 at 15:11
  • Yeah I marked your answe. Thanks for that! BTW, I'm curious to know why I'm getting everything in the format of (session="1423be9c3acccc90b384d06d0f1bb975", element="63af1628-b630-4a27-8684-33f679a54196")>] when printing 'job_list' rather than their dom structure? I wonder what 'session' and 'element' really are
    – Mansidak
    Feb 1 at 15:12
  • Yeah printing elements like that is not human readable. It's essentially the GUID for the session and element. You'd have to write some pretty fancy custom code to get it to print the full DOM. A quick suggestion would be to print element.get_attribute('innerHTML') and that will print the HTML of that particular element.
    – JeffC
    Feb 1 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.