[英]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-8
和jobtitle
是 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.