关于selenium调用浏览器的问题
在selenium调用Firefox时会出现:selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.
解决方案:
以Firefox为例:
方案一:
- 首先根据浏览器版本下载对应的geckodriver:https://github.com/mozilla/geckodriver/releases;
- 然后将解压后的文件放在Python的安装目录中的Scripts中.
方案二:
- 如果解压的文件放在别的目录下,可以在调用Firefox时参数中传入路径.
from selenium import webdriver
try:
browser = webdriver.Firefox(executable_path='C:\Program Files\Mozilla Firefox/geckodriver')
browser.get("http://www.baidu.com")
by_id = browser.find_element_by_id("u")
print(by_id)
finally:
browser.close()
selenium调用其它浏览器的解决方案同上.
附上各浏览器的WebDriver下载链接:
Chrome:http://npm.taobao.org/mirrors/chromedriver/
IE:http://selenium-release.storage.googleapis.com/index.html
Edge:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/