困扰我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