最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Can't get all the elements of a html class using selenium - Stack Overflow

programmeradmin1浏览0评论

I'm trying to get all elements with the class name "jobs-save-button__text" on LinkedIn's job search page. However, when I run the code, it only returns two elements. I expected it to return as many elements as there are job postings.

This is what I am trying to extract:

\<span aria-hidden="true" class="jobs-save-button__text"\>
Save
\</span\>

This is my code:

WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'jobs-save-button__text')))
save_button = driver.find_elements(By.CLASS_NAME, 'jobs-save-button__text')
save = [i for i in save_button]

This is the site: /?keywords=python%20developer&geoId=103644278

I tried changing the path by using

(By.XPATH, "//*[@class='jobs-save-button__text']") 

but still only extracts two elements. When I look through the html they all have the same class name so not sure why this isn't working. I even have a similar line extracting the job title using the same code which works fine. I've also tried time.sleep() before the code gets executed but didn't work.

job_title = driver.find_elements(By.CSS_SELECTOR, 'div div div a span strong')
title = [i.get_attribute('textContent').strip() for i in job_title]

I'm trying to get all elements with the class name "jobs-save-button__text" on LinkedIn's job search page. However, when I run the code, it only returns two elements. I expected it to return as many elements as there are job postings.

This is what I am trying to extract:

\<span aria-hidden="true" class="jobs-save-button__text"\>
Save
\</span\>

This is my code:

WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'jobs-save-button__text')))
save_button = driver.find_elements(By.CLASS_NAME, 'jobs-save-button__text')
save = [i for i in save_button]

This is the site: https://www.linkedin/jobs/search/?keywords=python%20developer&geoId=103644278

I tried changing the path by using

(By.XPATH, "//*[@class='jobs-save-button__text']") 

but still only extracts two elements. When I look through the html they all have the same class name so not sure why this isn't working. I even have a similar line extracting the job title using the same code which works fine. I've also tried time.sleep() before the code gets executed but didn't work.

job_title = driver.find_elements(By.CSS_SELECTOR, 'div div div a span strong')
title = [i.get_attribute('textContent').strip() for i in job_title]
Share Improve this question edited Mar 3 at 14:20 dqyix asked Mar 3 at 14:08 dqyixdqyix 11 bronze badge 2
  • Figured it out. I ran a for loop clicking on each job listing and then used save_button = driver.find_element(By.CLASS_NAME, 'jobs-save-button__text'). I guess this has something to do with the DOM? Not to sure though I'm new to programming and don't really understand how this works. – dqyix Commented Mar 3 at 15:15
  • You have to click on each job listing first. The two save buttons you found, one was visible and the other was not... they were not two job listings. The HTML for the entire page with all listings is not in the DOM, that's why you have to click each one first. – JeffC Commented Mar 3 at 15:30
Add a comment  | 

1 Answer 1

Reset to default 0

There are only two elements here ..

for every click on job posting different ones will be coming but thats dynamic so only 2 elements manifest at any give time for a single posting.

So you will write a loop, click on each job posting item //li[ div/div[contains(@class, "job-card")]], and get 2 elements each time and loop through next posting item again..

发布评论

评论列表(0)

  1. 暂无评论