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發(fā)起請求
bor.get('https://www.jd.com/')
sleep(1)
# 進行標(biāo)簽定位
search_input = bor.find_element_by_id('key')
# 向搜索框中錄入關(guān)鍵詞
search_input.send_keys("mac pro")
# 點擊搜索按鈕
btn = bor.find_element_by_xpath('//*[@id="search"]/div/div[2]/button')
btn.click()
sleep(2)
# 執(zhí)行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_*`已經(jīng)被移除了
把`bor.find_element_by_id('key')`換成`bor.find_element('id','key')`就行,其他幾個類似
?文章來源:http://www.zghlxwxcb.cn/news/detail-516752.html
from selenium import webdriver
from time import sleep
# 實例化一款瀏覽器
bor = webdriver.Chrome(executable_path='E:/chromedriver_win32/chromedriver.exe')
# 對指定的url發(fā)起請求
bor.get('https://www.jd.com/')
sleep(1)
# 進行標(biāo)簽定位
search_input = bor.find_element('id','key')
# 向搜索框中錄入關(guān)鍵詞
search_input.send_keys("mac pro")
# 點擊搜索按鈕
btn = bor.find_element('xpath', '//*[@id="search"]/div/div[2]/button')
btn.click()
sleep(2)
# 執(zhí)行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文章來源地址http://www.zghlxwxcb.cn/news/detail-516752.html
到了這里,關(guān)于Selenium - Python - AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_id‘的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!