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

时间:2019-10-16 23:40:18

标签: python selenium web-scraping dynamic

我正在尝试从动态职位列表中抓取职位。当我使用函数find_elements_by_class_name时,该函数不返回任何内容。我是硒的新手,所以我不确定是否只是在做错误的事情或误解了功能。

我要抓取的页面是: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)

3 个答案:

答案 0 :(得分:0)

好像是个错误? 这有效:

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

答案 1 :(得分:0)

尝试:

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

我已经为xpath按类切换了find元素,这样您就具有更大的灵活性,并且通常效果更好,建议您研究一下!

答案 2 :(得分:0)

根本原因:

col-md-8jobtitle是2个不同的类。当您使用find_element_by_class_name时,它将在内部将类名称转换为CSS选择器,并尝试查找该元素。

以下是find_element_by_class_name内部使用css的证据。

enter image description here

解决方案:

当Selenium内部使用css时,您必须确保将类合并在一起,意味着class1.class2.class3。用简单的术语replace all white spaces with single dot表示UI中的类名。

如何根据您的情况实施此操作:

您必须使用以下语法。

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