这些错误为我们提供了一些有关发生了什么错误的提示,如下所示:
[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()
您的程序在这里出错,因为与 key *
[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服务 ,如下所示:
class Service(service.Service):“”“管理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 倾向于在 当前工作目录中 自行创建文件,并且在缺少所需 情况下,它会出错,并显示以下消息:
初始化 GeckoDriver 与所需的参数executable_path
和log_path
与 有效的值(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
如果您打算geckodriver.log
在 Project Workspace中 创建,请确保所需的权限(chmod 777 Project 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