在做web UI 自动化时,chrome浏览器可以成功打开,但使用同样的方式,打开火狐浏览器却报错,报错信息如下:
Exception in thread Thread-1:
Traceback (most recent call last):
File "E:\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "E:\Python\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "E:/PyCharm/PythonProjects/lesson16_1/test_baidu.py", line 22, in test_baidu_search
driver = webdriver.Firefox('E:\PyCharm\PythonProjects\lesson16_1\geckodriver.exe')
File "E:\Python\Python38-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 151, in __init__
firefox_profile = FirefoxProfile(firefox_profile)
File "E:\Python\Python38-32\lib\site-packages\selenium\webdriver\firefox\firefox_profile.py", line 79, in __init__
shutil.copytree(self.profile_dir, newprof,
File "E:\Python\Python38-32\lib\shutil.py", line 546, in copytree
with os.scandir(src) as entries:
NotADirectoryError: [WinError 267] 目录名称无效。: 'E:\\PyCharm\\PythonProjects\\lesson16_1\\geckodriver.exe'
报错原因webdriver.Firefox()有很多参数,配置执行路径的参数不在第一个,直接用webdriver去启动已经不行了,需要加上关键字参数executable_path:
driverpath = r'E:\PyCharm\PythonProjects\lesson16_1\geckodriver.exe'
driver = webdriver.Firefox(executable_path=driverpath)