简体   繁体   English

Selenium Python | 'find_elements_by_class_name' 不返回任何内容

[英]Selenium Python | 'find_elements_by_class_name' returns nothing

I am trying to scrape job titles from a dynamic job listing.我正在尝试从动态职位列表中抓取职位名称。 When I use the function find_elements_by_class_name, the function doesn't return anything.当我使用 function find_elements_by_class_name 时,function 不会返回任何内容。 I'm new to selenium so i'm not sure if i'm simply doing something incorrectly or misunderstanding the functionality.我是 selenium 的新手,所以我不确定我只是做错了什么还是误解了功能。

The page im trying to scrape is: https://recruit.hirebridge.com/v3/CareerCenter/v2/?cid=7724我试图抓取的页面是: https://recruit.hirebridge.com/v3/CareerCenter/v2/?cid=7724

from selenium import webdriver
import time

#define the path for the chrome webdriver
chrome_path = r"C:/web/jobListing/chromedriver.exe"

#create a instance of the webdriver
driver = webdriver.Chrome(chrome_path)

driver.get("https://recruit.hirebridge.com/v3/CareerCenter/v2/?cid=7724")
time.sleep(10)
jobs = driver.find_elements_by_class_name("col-md-8 jobtitle")

print("starting print")
for job in jobs:
    print(job.text)

Seems like a bug?看起来像一个错误? This works:这有效:

jobs = driver.execute_script("""
  return document.getElementsByClassName("col-md-8 jobtitle")
""")

Try:尝试:

jobs = driver.find_elements_by_xpath("//div[@class='col-md-8 jobtitle']/a")

I've switched the find element by class for xpath, this way you have more flexibility and it generally works better, I suggest you look into it!我已将 class 的查找元素切换为 xpath,这样您具有更大的灵活性并且通常效果更好,建议您研究一下!

Root Cause:根本原因:

col-md-8 and jobtitle are 2 different classes. col-md-8jobtitle是 2 个不同的类。 When you use find_element_by_class_name it will internally convert the class name to a css selector and try to find the element.当您使用find_element_by_class_name时,它会在内部将 class 名称转换为 css 选择器并尝试查找元素。

Below is the evidence where find_element_by_class_name uses the css internally.以下是find_element_by_class_name在内部使用css的证据。

在此处输入图像描述

Solution:解决方案:

As Selenium internally uses the css you have to make sure the classes are clubbed together meaning class1.class2.class3 .由于 Selenium 在内部使用css ,因此您必须确保将这些类组合在一起,这意味着class1.class2.class3 In simple terms replace all white spaces with single dot in the class name from UI.简单来说,用 UI 中 class 名称中的replace all white spaces with single dot

How to implement this to your situation:如何根据您的情况实施:

You have to use the below syntax.您必须使用以下语法。

driver.find_element_by_class_name('col-md-8.jobtitle')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python / Selenium-具有长类名的find_elements_by_class_name - Python/Selenium - find_elements_by_class_name with long class name 使用Python在Selenium中使用find_elements_by_class_name查找href属性 - Find href attributes with find_elements_by_class_name in Selenium with Python Python:Selenium WebDriver find_elements_by_class_name - Python: Selenium WebDriver find_elements_by_class_name Python Selenium find_elements_by_class_name 错误 - Python Selenium find_elements_by_class_name Error 如何让 find_elements_by_class_name 在 Python Selenium 中工作? - How to get find_elements_by_class_name to work in Python Selenium? Selenium find_elements_by_class_name 未返回完整列表 - Selenium find_elements_by_class_name not returning full list 过滤由 selenium 通过.find_elements_by_class_name 使用 python 选择的元素 - Filtering elements selected by selenium through .find_elements_by_class_name using python python3硒find_elements_by_class_name不能单击每个项目 - python3 selenium find_elements_by_class_name cannot click on each item 如何使用 Selenium 和 Python 使用 find_elements_by_class_name() 单击元素 - How to click on element using find_elements_by_class_name() using Selenium and Python 使用 Selenium “find_elements_by_class_name” 从网站检索数据 - Using Selenium “find_elements_by_class_name” to retrieve data from website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM