python爬虫遇到的问题:selenium引用chromedriver出现的问题

traceback

Traceback (most recent call last):
  File "D:\Anaconda35\lib\site-packages\selenium\webdriver\chrome\service.py", line 66, in start
    self.service_args, env=env, stdout=PIPE, stderr=PIPE)
  File "D:\Anaconda35\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "D:\Anaconda35\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Documents/Desktop/python/网络爬虫/cve/getCveLists.py", line 16, in <module>
    driver = webdriver.Chrome(executable_path=chromedriver,chrome_options=chrome_options)#对应的chromedriver的放置目录
  File "D:\Anaconda35\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 61, in __init__
    self.service.start()
  File "D:\Anaconda35\lib\site-packages\selenium\webdriver\chrome\service.py", line 73, in start
    os.path.basename(self.path), docs_msg)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

通过查询,定位了原因,原来是因为我传递chromedriver的路径时因为直接是用的windows的反斜杠,所以无法被识别,

chromedriver = 'C:\Program Files(x86)\Google\Chrome\Application\chromedriver.exe'
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(executable_path=chromedriver,chrome_options=chrome_options)

这种路径好像一般python识别会出错,因此只需把斜杠进行转换即可,如下面,

chromedriver = r'C:/Program Files(x86)/Google/Chrome/Application\chromedriver.exe'

但是我再运行还是出现上面的问题,我就猜测可能是权限的问题,有可能chrome所在的文件夹访问需要管理员权限,所以我查看了该文件夹下的属性,找到安全这一栏
这里写图片描述

发现组或用户名里面没有我的用户,说明我的用户无法直接对这个文件夹进行操作(需要管理员权限),所以解决办法应该是将chromedriver移到其他位置,比如我就是放大E盘,然后把路径改成E盘的路径,
再用就没问题了。

PS:这里可以吐槽一下网上那些说把chromedriver放到chrome文件夹下面的博客!!!!!

猜你喜欢

转载自blog.csdn.net/qq_36304617/article/details/82530994