selenium:4.7.2
chromeDriver:108.0.5359.22 下载
最近看selenium遇到了这个报错
看的是这个教程,代码也是里面的。
from selenium import webdriver
from time import sleep
# 实例化一款浏览器
bor = webdriver.Chrome(executable_path='chromedriver.exe')
# 对指定的url发起请求
bor.get('https://www.jd.com/')
sleep(1)
# 进行标签定位
search_input = bor.find_element_by_id('key')
# 向搜索框中录入关键词
search_input.send_keys("mac pro")
# 点击搜索按钮
btn = bor.find_element_by_xpath('//*[@id="search"]/div/div[2]/button')
btn.click()
sleep(2)
# 执行js,让滚轮向下滚动
bor.execute_script('window.scrollTo(0, document.body.scrollHeight)')
sleep(2)
page_text = bor.page_source
print(page_text)
bor.quit()
报错如下
e:\project\demo02\main.py:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
bor = webdriver.Chrome(PATH)
DevTools listening on ws://127.0.0.1:65430/devtools/browser/52ab5d7c-a0b0-4488-9a8c-ed83a07e7636
[25936:23208:1217/010556.344:ERROR:socket_manager.cc(138)] Failed to resolve address for stun.services.mozilla.com., errorcode: -105
[25936:23208:1217/010556.346:ERROR:socket_manager.cc(138)] Failed to resolve address for stun.services.mozilla.com., errorcode: -105
Traceback (most recent call last):
File "e:\project\demo02\main.py", line 12, in <module>
search_input = bor.find_element_by_id('key')
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'
后来查到原因是`find_element_*`已经被移除了
把`bor.find_element_by_id('key')`换成`bor.find_element('id','key')`就行,其他几个类似
文章来源:https://www.toymoban.com/news/detail-516752.html
from selenium import webdriver
from time import sleep
# 实例化一款浏览器
bor = webdriver.Chrome(executable_path='E:/chromedriver_win32/chromedriver.exe')
# 对指定的url发起请求
bor.get('https://www.jd.com/')
sleep(1)
# 进行标签定位
search_input = bor.find_element('id','key')
# 向搜索框中录入关键词
search_input.send_keys("mac pro")
# 点击搜索按钮
btn = bor.find_element('xpath', '//*[@id="search"]/div/div[2]/button')
btn.click()
sleep(2)
# 执行js,让滚轮向下滚动
bor.execute_script('window.scrollTo(0, document.body.scrollHeight)')
sleep(2)
page_text = bor.page_source
print(page_text)
bor.quit()
参考:Selenium - Python - AttributeError: 'WebDriver' object has no attribute 'find_element_by_name' - Stack Overflow文章来源地址https://www.toymoban.com/news/detail-516752.html
到了这里,关于Selenium - Python - AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_id‘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!