环境 win10 + python3.8 通过selenium 库打开chrome浏览器时报错如下:
Traceback (most recent call last): File “C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\common\service.py”, line 71, in start self.process = subprocess.Popen(cmd, env=self.env, File “C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\subprocess.py”, line 804, in init errread, errwrite) = self._get_handles(stdin, stdout, stderr) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\subprocess.py”, line 1169, in _get_handles c2pwrite = msvcrt.get_osfhandle(self._get_devnull()) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\subprocess.py”, line 952, in _get_devnull self._devnull = os.open(os.devnull, os.O_RDWR) FileNotFoundError: [Errno 2] No such file or directory: ‘nul’
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File “.\jd.py”, line 93, in jd = JD_Verification_code() File “.\jd.py”, line 22, in init self.driver = webdriver.Chrome(options=options) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py”, line 70, in init super(WebDriver, self).init(DesiredCapabilities.CHROME[‘browserName’], “goog”, File “C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chromium\webdriver.py”, line 90, in init self.service.start() File “C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\common\service.py”, line 81, in start raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
报错信息给的解决方案是下载对应版本的chromedriver.exe到环境变量里面去。 实际下载后,并没有解决问题。
以下提出我自己的思路。
根据报错信息定位到python库源码中subprocess.py 952行。源码如下: def _get_devnull(self): if not hasattr(self, ‘_devnull’): self._devnull = os.open(os.devnull, os.O_RDWR) return self._devnull 这里是打开os.devnull。报错信息提示其值为nul。实际debug之后也确实如此。 那根据我自己的经验,源码本意应该是打开设备节点nul,把不需要的log等信息输出到该节点。如果是在linux系统上应该是没问题的,因为linux有这样一个null设备节点用来输出垃圾信息的。win10上可能没有这个节点,所以报这个错。 那我的解决方案是给它赋值一个实际存在的txt文件,让系统能正常打开并写入信息就可以了。 1.我对库源码进行改造如下: def _get_devnull(self): if not hasattr(self, ‘_devnull’): self._devnull = os.open(‘c:/null.txt’, os.O_RDWR) return self._devnull
2.然后再在c:/盘新建一个null.txt。再运行代码能够完美地正常运行。
运行完成后不出我所料发现这个null.txt里有一些运行时log:
如果各位遇到相同的问题,不妨参考本人的解决思路。如果完美解决,不妨给点个赞。
|