pyinstaller打包selenium报错找不到webdriver

遇到的情况:

使用pyinstaller -w -F打包selenium
在大部分机器上运行正常 但是少数机器上运行报错
selenium版本:3.141.0

报错内容:

Message: ‘chromedriver.exe’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

关键代码:

chrome_location = './Chrome/chrome.exe'
chrome_path = "./Chrome/chromedriver.exe"
browser = webdriver.Chrome(chrome_path,options=options)

在网上查了许多方法,比如将chromedriver和exe一起打包等等都不管用

后来发现是打包时加了-w参数的话 导致python安装路径下的\selenium\webdriver\common\service.py
里面的subprocess.Popen失效

解决办法:

subprocess.Popen修改为:

self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=platform.system() != 'Windows',
                                            # stdout=self.log_file,
                                            # stderr=self.log_file,
                                            stdin=PIPE,
                                            creationflags=134217728,
                                            shell=True,
                                            stdout=subprocess.PIPE,
                                            stderr=subprocess.STDOUT)

再将调用selenium的代码改为

from os import getcwd
chrome_location = getcwd() + '/Chrome/chrome.exe'
chrome_path = getcwd() + "/Chrome/chromedriver.exe"
browser = webdriver.Chrome(chrome_path,options=options)

即可解决

你可能感兴趣的:(selenium,chrome,python)