我在通过selenium python抓取内容时出错



我正在上抓取作业结果的标题https://www.indeed.ae/jobs-in-dubai通过硒。我想。文本不起作用。我正在通过selenium运行代码,该代码进入主网站,输入选择性关键字,然后从结果中抓取所有标题。但是我有错误,我该如何解决这个错误

这是我的代码

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
Path = "C:Program Files (x86)chromedriver.exe"
driver = webdriver.Chrome(Path)
driver.get("https://indeed.ae/")
print(driver.title)
search = driver.find_element_by_name("l")
search.send_keys("Dubai")
search.send_keys(Keys.RETURN)

try:
td = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "resultsCol"))
)
divs = td.find_elements_by_tag_name("div")
for div in divs:
header = div.find_element_by_class_name("title")
print(header)
finally:
driver.quit()
driver.quit()

我得到以下错误

Job Search | Indeed
Traceback (most recent call last):
File "C:/Users/hp/Desktop/python projects/selenium-pycharm/selenium-bot.py", line 24, in <module>
header = div.find_element_by_class_name("title")
File "C:UsershpDesktoppython projectsselenium-pycharmvenvlibsite-packagesseleniumwebdriverremotewebelement.py", line 398, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:UsershpDesktoppython projectsselenium-pycharmvenvlibsite-packagesseleniumwebdriverremotewebelement.py", line 659, in find_element
{"using": by, "value": value})['value']
File "C:UsershpDesktoppython projectsselenium-pycharmvenvlibsite-packagesseleniumwebdriverremotewebelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:UsershpDesktoppython projectsselenium-pycharmvenvlibsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsershpDesktoppython projectsselenium-pycharmvenvlibsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".title"}
(Session info: chrome=83.0.4103.116)

Process finished with exit code 1

提前感谢

您找不到标题,因为您从resultsCol中获取了所有div。这意味着有些div有标题,有些则没有。

试试这个:

try:
td = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "resultsCol"))
)
divs = td.find_elements_by_tag_name("div")
#print(divs)
for div in divs:
try:
header = div.find_element_by_class_name("title")
print(header.text)
except:
continue
finally:
driver.quit()
driver.quit()

将标题作为输出:

Receptionist
Administrative Assistant/ Document Controller
RECEPTIONIST
ADMIN OFFICER IN UAE
Data Entry Assistant (Fresh Graduate)
Receptionist
Replenishment Associate - Light Household - Hypermarket
DOCUMENT CONTROLLER
School Administrative Assistant - Dubai
ACCOUNTANT

相关内容

  • 没有找到相关文章

最新更新