selenium 属性错误:'WebDriver'对象没有'find_element_by_css_selector'属性

pgpifvop  于 2022-11-24  发布在  其他
关注(0)|答案(4)|浏览(664)

我正在遵循this建立一个LinkedIn工作数据的刮刀。
下面是我的代码:

from selenium import webdriver
import time
import pandas as pd

url = 'https://www.linkedin.com/jobs/search?keywords=&location=San%20Francisco%2C%20California%2C%20United%20States&locationId=&geoId=102277331&f_TPR=&distance=100&position=1&pageNum=0'

wd = webdriver.Chrome(executable_path=r'/Users/voi/chromedriver')
wd.get(url)

no_of_jobs = int(wd.driver.find_element_by_css_selector('h1>span').get_attribute('innerText'))

我已经看到了this,并尝试了解决方案,但收到了类似的错误,除了关于WebDriver对象没有驱动程序属性。
以下是完整的错误消息:

cd /Users/voi ; /usr/bin/env /usr/local/bin/python3 /Users/voi/.vscode/extensions/ms-python.python-2
022.8.1/pythonFiles/lib/python/debugpy/launcher 59402 -- /Users/voi/jobscrape.py 
/Users/voi/jobscrape.py:7: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  wd = webdriver.Chrome(executable_path=r'/Users/voi/chromedriver')
Traceback (most recent call last):
  File "/Users/voi/jobscrape.py", line 10, in <module>
    no_of_jobs = int(wd.find_element_by_css_selector('h1>span').get_attribute('innerText'))
AttributeError: 'WebDriver' object has no attribute 'find_element_by_css_selector'
zpjtge22

zpjtge221#

好了,我回答了我自己的问题。单独的方法find_element_by_* 已经被find_element代替了,例如:

no_of_jobs = int(wd.find_element(By.CSS_SELECTOR, 'h1>span'))

更多信息请参阅here

k5ifujac

k5ifujac2#

Selenium刚刚在4.3.0版本中删除了该方法。请参阅更改:https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES

Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* Deprecated Opera support has been removed (#10630)
* Fully upgraded from python 2x to 3.7 syntax and features (#10647)
* Added a devtools version fallback mechanism to look for an older version when mismatch occurs (#10749)
* Better support for co-operative multi inheritance by utilising super() throughout
* Improved type hints throughout

您现在需要用途:

driver.find_element("css selector", SELECTOR)

在您的示例中,可以用途:

no_of_jobs = int(wd.find_element("css selector", "h1 > span").get_attribute("innerText"))

为了提高可靠性,应考虑将WebDriverWaitvisibility_of_element_located结合使用。
下面是它的外观:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

# ...

element = WebDriverWait(wd, 10).until(
    EC.visibility_of_element_located(("css selector", "h1 > span"))
)
no_of_jobs = int(element.get_attribute("innerText"))
pnwntuvh

pnwntuvh3#

Selenium最近推出了removed,16个被弃用的find_element(s)_by_x函数,支持通用的find_elementfind_elements函数,它们将“by”部分作为它们的第一个参数。
要更新代码,可以使用IDE得全部查找并替换功能替换以下16个搜索词:

.find_element_by_class_name(
.find_element(By.CLASS_NAME, 

.find_element_by_css_selector(
.find_element(By.CSS_SELECTOR, 

.find_element_by_id(
.find_element(By.ID, 

.find_element_by_link_text(
.find_element(By.LINK_TEXT, 

.find_element_by_name(
.find_element(By.NAME, 

.find_element_by_partial_link_text(
.find_element(By.PARTIAL_LINK_TEXT, 

.find_element_by_tag_name(
.find_element(By.TAG_NAME, 

.find_element_by_xpath(
.find_element(By.XPATH, 

.find_elements_by_class_name(
.find_elements(By.CLASS_NAME, 

.find_elements_by_css_selector(
.find_elements(By.CSS_SELECTOR, 

.find_elements_by_id(
.find_elements(By.ID, 

.find_elements_by_link_text(
.find_elements(By.LINK_TEXT, 

.find_elements_by_name(
.find_elements(By.NAME, 

.find_elements_by_partial_link_text(
.find_elements(By.PARTIAL_LINK_TEXT, 

.find_elements_by_tag_name(
.find_elements(By.TAG_NAME, 

.find_elements_by_xpath(
.find_elements(By.XPATH,

如果还没有导入By,您还需要这样做:

from selenium.webdriver.common.by import By
wko9yo5t

wko9yo5t4#

为了补充来自@m.lekk(https://stackoverflow.com/a/72854301/7733418)的答案,我还尝试使用dir()来获取对象的所有属性,并找到包含所需信息的text属性。

相关问题