iT邦幫忙

0

請教關於 selenium 在pychream 中不起作用?

  • 分享至 

  • xImage

已安裝完selenium
但是在pychream中執行卻出現以下錯誤
因為不知道是不是環境變數沒設好造成的
所以將環境變量一起截圖上來
請前輩們幫幫忙..!

https://ithelp.ithome.com.tw/upload/images/20230901/20134660414jmy7oxh.jpg
https://ithelp.ithome.com.tw/upload/images/20230901/20134660YDkSgGjPeb.jpg
path
https://ithelp.ithome.com.tw/upload/images/20230901/20134660xFr0xJeLRF.jpg
pychram
https://ithelp.ithome.com.tw/upload/images/20230901/20134660F8fALBDzBp.jpg
pychram Commu
https://ithelp.ithome.com.tw/upload/images/20230901/20134660dNkSIU9Iky.jpg

import os
import time
from selenium import webdriver
driver = webdriver.Chrome("C:\chromedriver-win64\chromedriver.exe")
driver.get("https://tw.yahoo.com/")
time.sleep(5)
driver.close()

錯誤訊息

C:\ProgramData\Anaconda3\python.exe C:\Users\FANMO4F\PycharmProjects\pythonProject2\005.py 
Traceback (most recent call last):
  File "C:\Users\FANMO4F\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
  File "C:\Users\FANMO4F\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\common\selenium_manager.py", line 71, in driver_location
    browser = options.capabilities["browserName"]
AttributeError: 'str' object has no attribute 'capabilities'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\FANMO4F\PycharmProjects\pythonProject2\005.py", line 4, in <module>
    driver = webdriver.Chrome("C:\chromedriver-win64\chromedriver.exe")
  File "C:\Users\FANMO4F\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
    super().__init__(
  File "C:\Users\FANMO4F\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\chromium\webdriver.py", line 51, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
  File "C:\Users\FANMO4F\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\common\driver_finder.py", line 40, in get_path
    msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager."
AttributeError: 'str' object has no attribute 'capabilities'
看更多先前的討論...收起先前的討論...
檢查你的chrome driver 路徑是否正確
檢查你chrome driver 版本 跟你的 chrome 版本是否相符
另外
我看一下別人用法
service = Service(executable_path='./chromedriver.exe')
driver = webdriver.Chrome(service=service)
跟你的有差,建議你改一下
fdfanmo iT邦新手 5 級 ‧ 2023-09-01 12:39:19 檢舉
chrome driver 版本 跟你的 chrome 版本是否相符--不相符
https://googlechromelabs.github.io/chrome-for-testing/#stable
這邊116.0.5845.140好像還沒開放下載
那上面那個網址放的
https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/116.0.5845.96/win64/chrome-win64.zip
是要下載下來嗎?
版本要相同
不然會有問題
只需要對前面三個版本號就可以了
fdfanmo iT邦新手 5 級 ‧ 2023-09-02 08:09:57 檢舉
謝謝asd3733653大的幫忙.我找到問題了.要將Chromedriver安裝到python下的script資料夾下再設定環境變數這樣就會出現瀏覽器了.
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
熊熊工程師
iT邦研究生 2 級 ‧ 2023-09-08 13:21:01
最佳解答

我記得 selenium 最新版好像不需要下載 driver,直接調用 Chrome() 就會自己幫你處理了,可以試試看,或是可以使用 webdriver-manager 這個套件

fdfanmo iT邦新手 5 級 ‧ 2023-09-11 07:57:53 檢舉

謝謝熊熊工程師的解答.我在官方看到的說明好像也是這樣的.這樣做確實較為方便.

我要發表回答

立即登入回答