selenium不适用于Firefox或Chrome

Aut*_*Job 6 python firefox selenium

我正在尝试学习python web scraping,但我不能让selenium与任何一个浏览器一起工作.

from selenium import webdriver
browser = webdriver.Firefox()
Run Code Online (Sandbox Code Playgroud)

这是我的所有代码,我得到了一个错误.

Traceback (most recent call last):
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "H:\codingpractice\python\python challenge.com.py", line 2, in <module>
    browser = webdriver.Firefox()
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
    self.service.start()
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00A11350>>
Traceback (most recent call last):
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
    self.stop()
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Run Code Online (Sandbox Code Playgroud)

我已经尝试了在互联网上找到的所有内容,即添加代码路径

from selenium import webdriver
browser = webdriver.Firefox("C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
Run Code Online (Sandbox Code Playgroud)

在我的环境变量中添加PATH.我似乎无法弄清楚这一点......

pag*_*gep 9

对于Firefox和Chrome,您现在需要下载geckodriver/chromedriver.这些驱动程序是您安装的浏览器和selenium之间进行通信所必需的.所以你需要:

  • 为python安装selenium(pip install selenium)
  • 下载您要使用的浏览器的驱动程序(chromedriver,geckodriver,operadriver等)
  • 安装要在系统上使用的浏览器(可能已经有了这个)

现在,您可以将geckodriver添加到您的路径中,如此anwser中所述.或者您可以直接在代码中进行设置,如下所示:

丁目: driver = webdriver.Chrome(executable_path='/path/to/chromedriver.exe')

火狐: driver = webdriver.Firefox(executable_path='/opt/geckoDriver/geckodriver.exe')