Centos7下搭建Selenium+firefox+python环境的爬坑记录

Centos7下搭建Selenium+firefox+python环境的爬坑记录

首先说明一下我搭建的环境主要是:

  • python2.7

  • firefox62.0.3

  • selenium这个我直接是执行的

  • pip install selenium
    
  • geckodriver v0.19.0

我的测试程序就是官网的,稍微修改了一下:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('/usr/local/firefox/firefox')#这个路径一定要写
driver = webdriver.Firefox(firefox_binary=binary)
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

说一说我今天下午遇到的错误:

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

这个错误比较弱智。就是去官网下载geckodriver对应版本就好,我刚开始下载的是22,后来由于不匹配所以换成了0.19.0。

下载地址:

具体的下载安装详见:

https://selenium-python.readthedocs.io/installation.html

selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process

解决:更新我的firefox60.1->62.0.3

selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

解决:加入了两行代码:

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('/usr/local/firefox/firefox')#这个路径一定要写
driver = webdriver.Firefox(firefox_binary=binary)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status: 1

解决:因为我在Xshell下运行,没有图形界面。转到Gnome里面就成功啦。

当然,网上也有解决无图形界面下的运行:

https://stackoverflow.com/questions/46809135/webdriver-exceptionprocess-unexpectedly-closed-with-status-1

总的来说,官网+stackoverflow+google缺一不可,人还是要有耐心~

猜你喜欢

转载自blog.csdn.net/nicezheng_1995/article/details/82953765