iT邦幫忙

0

webdriver問題

  • 分享至 

  • xImage

請問各位大佬,我按照影片教學的方法輸入程式碼:

from selenium import webdriver
web = webdriver.Chrome()
web.get("https://www.dcard.tw/f")
search = web.find_element_by_name("query")
search.send_keys("博愛座")


然後運行之後就輸出這些:

/usr/local/bin/python3 "/Users/qiufuwei/Library/Mobile Documents/com~apple~CloudDocs/python/simpledriver.py"
qiufuwei@qiufuweideAir python % /usr/local/bin/python3 "/Users/qiufuwei/Library/Mobile Documents/com~apple~CloudDocs/python/simpledriver.py"
Traceback (most recent call last):
File "/Users/qiufuwei/Library/Mobile Documents/com~apple~CloudDocs/python/simpledriver.py", line 4, in
web.get("https://www.dcard.tw/f")
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 447, in get
self.execute(Command.GET, {'url': url})
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot determine loading status
from unknown error: unexpected command response
(Session info: chrome=104.0.5112.79)
Stacktrace:
0 chromedriver 0x0000000100899d14 chromedriver + 3792148
1 chromedriver 0x0000000100830828 chromedriver + 3360808
2 chromedriver 0x00000001005310b8 chromedriver + 217272
3 chromedriver 0x0000000100520124 chromedriver + 147748
4 chromedriver 0x000000010051f8f4 chromedriver + 145652
5 chromedriver 0x000000010051ef50 chromedriver + 143184
6 chromedriver 0x000000010051e058 chromedriver + 139352
7 chromedriver 0x000000010051e4f8 chromedriver + 140536
8 chromedriver 0x000000010052a394 chromedriver + 189332
9 chromedriver 0x0000000100537a20 chromedriver + 244256
10 chromedriver 0x000000010051e79c chromedriver + 141212
11 chromedriver 0x000000010053769c chromedriver + 243356
12 chromedriver 0x0000000100589d74 chromedriver + 580980
13 chromedriver 0x0000000100556318 chromedriver + 369432
14 chromedriver 0x000000010086f1e8 chromedriver + 3617256
15 chromedriver 0x0000000100873678 chromedriver + 3634808
16 chromedriver 0x0000000100877c6c chromedriver + 3652716
17 chromedriver 0x0000000100874110 chromedriver + 3637520
18 chromedriver 0x00000001008527ac chromedriver + 3499948
19 chromedriver 0x000000010088bbf0 chromedriver + 3734512
20 chromedriver 0x000000010088bd54 chromedriver + 3734868
21 chromedriver 0x00000001008a0558 chromedriver + 3818840
22 libsystem_pthread.dylib 0x00000001ad84026c _pthread_start + 148
23 libsystem_pthread.dylib 0x00000001ad83b08c thread_start + 8


而且也不會按照我寫的找到搜尋框並輸入“博愛座”
請問我是哪裡寫錯了嗎,還請各位大佬指教 謝謝
系統MacOS montery 12.5

使用VS code:
版本: 1.70.0 (Universal)
提交: da76f93349a72022ca4670c1b84860304616aaa2
日期: 2022-08-04T04:38:55.829Z
電子: 18.3.5
Chromium: 100.0.4896.160
Node.js: 16.13.2
V8: 10.0.139.17-electron.0
作業系統: Darwin arm64 21.6.0

froce iT邦大師 1 級 ‧ 2022-08-12 08:46:40 檢舉
https://medium.com/marketingdatascience/selenium%E6%95%99%E5%AD%B8-%E4%B8%80-%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8webdriver-send-keys-988816ce9bed

你要先下載webdriver,然後如果還有問題,指定webdriver的絕對路徑。

web = webdriver.Chrome("你上面下載的exe檔")
fuwei iT邦新手 5 級 ‧ 2022-08-14 05:25:42 檢舉
謝謝前輩??
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
Xiang1009
iT邦研究生 4 級 ‧ 2022-08-14 02:58:05

下載chromedriver的網址如下:
https://sites.google.com/chromium.org/driver/downloads?authuser=0
記得找跟自己chrome瀏覽器版本一致的chromedriver下載(裡面還有分不同作業系統)

解壓縮後,在程式碼裡指定chromedriver.exe的路徑,就可以正常跳出dcard視窗並在搜尋打上"博愛座",下面是我測試時的檔案路徑。
https://ithelp.ithome.com.tw/upload/images/20220814/20135354xoNAG2RlaL.png

from selenium import webdriver
web = webdriver.Chrome('chromedriver.exe') //這邊要改成chromedriver的路徑
web.get("https://www.dcard.tw/f")
search = web.find_element("name", "query")
search.send_keys("博愛座")

https://ithelp.ithome.com.tw/upload/images/20220814/20135354ez7hf3NDr3.png

補充 : 如果你的Selenium安裝的是4.3.0或更新的版本,那search = web.find_element_by_name("query")這行就會出錯,因為方法已經被移除,要改成search = web.find_element("name", "query")

詳情見stackoverflow這篇

fuwei iT邦新手 5 級 ‧ 2022-08-14 05:27:18 檢舉

好的 謝謝前輩

我要發表回答

立即登入回答