通过 Flask/Python 运行 Selenium 时收到以下错误
browser = webdriver.Firefox()
[Wed Mar 07 03:02:27.719608 2018] [:error] [pid 21555] [client 108.162.250.6:36139] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 151, in __init__
[Wed Mar 07 03:02:27.719611 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_path=log_path)
[Wed Mar 07 03:02:27.719614 2018] [:error] [pid 21555] [client 108.162.250.6:36139] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/service.py", line 44, in __init__
[Wed Mar 07 03:02:27.719617 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
[Wed Mar 07 03:02:27.719620 2018] [:error] [pid 21555] [client 108.162.250.6:36139] IOError: [Errno 13] Permission denied: 'geckodriver.log'
函数是
def get_index(api_key):
if str(api_key)!=the_api_key:
return 401
base_url = 'www.google.com'
browser = webdriver.Firefox()
browser.get(base_url)
html = browser.page_source
return html
如果我直接转到应用程序目录并运行脚本 (python run.py
),那么我不会收到错误。
基于此,通过 Flask 运行时,日志文件似乎不可写,但文件应该位于何处?
geckdriver
可执行文件安装在 /usr/local/bin/
最佳答案
这些错误提示我们发生了什么错误,如下所示:
[Wed Mar 07 03:02:27.719608 2018] [:error] [pid 21555] [client 108.162.250.6:36139] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 151, in __init__
[Wed Mar 07 03:02:27.719611 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_path=log_path)
根据源代码,GeckoDriver 使用两个默认参数 executable_path
和 log_path=log_path
启动,如下所示:
if capabilities.get("marionette"):
capabilities.pop("marionette")
self.service = Service(executable_path, log_path=log_path)
self.service.start()
您的程序在此处出错,因为 Value log_path(log_file
)对应于 Key log_path 不可编辑(可附加),最终失败:
[Wed Mar 07 03:02:27.719617 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
[Wed Mar 07 03:02:27.719620 2018] [:error] [pid 21555] [client 108.162.250.6:36139] IOError: [Errno 13] Permission denied: 'geckodriver.log'
根据源代码,GeckoDriver 服务默认启动如下:
类服务(服务。服务): """管理启动和停止的对象 壁虎驱动程序。"""
def __init__(self, executable_path, port=0, service_args=None,
log_path="geckodriver.log", env=None):
"""Creates a new instance of the GeckoDriver remote service proxy.
GeckoDriver provides a HTTP interface speaking the W3C WebDriver
protocol to Marionette.
:param log_path: Optional path for the GeckoDriver to log to.
Defaults to _geckodriver.log_ in the current working directory.
"""
log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
这意味着如果您不通过您的程序显式传递 geckodriver.log
的位置,Geckodriver 往往会在 中自行创建一个文件当前工作目录 并且在缺少所需的权限 时出错并显示消息 Permission denied: 'geckodriver.log'
解决方案
首要的一点是检查您正在使用的二进制文件之间的兼容性。
- 确保您使用的是 Selenium-Python Client v3.10.0 , GeckoDriver v0.19.1和 Firefox Quantum v58.0.2
一个解决方案是:
使用所需参数
executable_path
和log_path
初始化 GeckoDriver 以及 有效值(chmod 777geckodriver .log
) 如下:def get_index(api_key): if str(api_key)!=the_api_key: return 401 base_url = 'www.google.com' browser = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver", log_path="/path/to/geckodriver.log") browser.get(base_url) html = browser.page_source return html
如果您打算在 Project Workspace 中创建
geckodriver.log
,请确保获得所需的权限(chmod 777Project Workspace
)如下:def get_index(api_key): if str(api_key)!=the_api_key: return 401 base_url = 'www.google.com' browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver') browser.get(base_url) html = browser.page_source return html
关于python - 运行 Python/Selenium 时出现 IOError : [Errno 13] Permission denied: 'geckodriver. 日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49146993/