硒Python | 'find_elements_by_class_name'不返回任何内容 [英] Selenium Python | 'find_elements_by_class_name' returns nothing
问题描述
我正在尝试从动态职位列表中抓取职位。当我使用功能find_elements_by_class_name时,该功能不返回任何内容。我是硒的新手,所以不确定我是只是做错了什么还是误解了功能。
我要抓取的页面是:
解决方案:
由于Selenium内部使用 css
,因此您必须确保将类合并在一起,这意味着 class1.class2.class3
。用简单的术语在UI的类名中用单点替换所有空格
。
如何根据您的情况实施此操作:
您必须使用以下语法。
driver.find_element_by_class_name('col-md-8.jobtitle')
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. I'm new to selenium so i'm not sure if i'm simply doing something incorrectly or misunderstanding the functionality.
The page im trying to scrape is: 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)
Root Cause:
col-md-8
and jobtitle
are 2 different classes. 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.
Below is the evidence where find_element_by_class_name
uses the css
internally.
Solution:
As Selenium internally uses the css
you have to make sure the classes are clubbed together meaning class1.class2.class3
. In simple terms replace all white spaces with single dot
in the class name from UI.
How to implement this to your situation:
You have to use the below syntax.
driver.find_element_by_class_name('col-md-8.jobtitle')
这篇关于硒Python | 'find_elements_by_class_name'不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!