困擾我3天的問題,竟然是dns導致的


sys:macOS Big Sur 版本 11.2.3

python: 3.8.5

selenium: 3.141.0

Chrome: 版本 90.0.4430.72(正式版本) (x86_64)

Firefox: 87.0 (64 位)

chromedriver: ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429})

geckodriver 0.29.1 (970ef713fe58 2021-04-08 23:34 +0200)

 

最近初看selenium,在python上進行自動化測試,就卡在啟動上,第一個demo就測不過去

pip安裝了selenium,也下載了chromedriver和geckodriver,也按照官網的要求把driver都放在了/usr/local/bin/目錄下

然后把下面代碼存入文件test.py

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://baidu.com')

然后執行 python test.py,不論使用webdriver.Chrome()還是webdriver.Firefox(),以及在這個函數里面放置各種參數,最終都會報如下錯誤:

Traceback (most recent call last):
  File "pro/web_scraping/test2.py", line 3, in <module>
    browser = webdriver.Firefox()
  File "/Users/xxx/.local/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
    self.service.start()
  File "/Users/xxx/.local/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 104, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver

 

試過各種方法,找個各種解決方案,內網外網都翻了許多答案,都沒有解決問題

 

今天甚至我nodejs都做了個demo去測試,然后成功了。然后就更蛋疼了。。。

 

今天看到這個帖子,豁然開朗

ping localhost,然后沒ping通

(base2) ➜  ~ ping localhost
ping: cannot resolve localhost: Unknown host
(base2) ➜  ~ ping localhost
ping: cannot resolve localhost: Unknown host
(base2) ➜  ~ ping localhost
ping: cannot resolve localhost: Unknown host
(base2) ➜  ~ ping localhost
ping: cannot resolve localhost: Unknown hosts

 

甚至不死心,還多試了幾次,在配置了hosts之后,再ping了一次通了,發現能吊起來Chrome和Firefox了,Cool

 

 

 

 

 

 

再次運行python test.py

 

 

 

 

 

 

 

 

因為使用hosts,推薦個軟件:https://apps.apple.com/cn/app/helm-hosts-%E6%96%87%E4%BB%B6%E7%AE%A1%E7%90%86/id1099472017?ign-mpt=uo%3D8&mt=12

 

 

 

 

參考文章:

https://blog.csdn.net/Excaliburrr/article/details/79164163

https://www.zhihu.com/question/19608612

https://stackoverflow.com/questions/50727595/selenium-common-exceptions-webdriverexception-message-can-not-connect-to-the-s

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM