【问题标题】:Trying to use chromedriver but get Exception AttributeError: "'Service' object has no attribute 'log_file'"尝试使用 chromedriver 但得到 Exception AttributeError: "'Service' object has no attribute 'log_file'"
【发布时间】:2017-03-09 22:12:31
【问题描述】:

我有这个代码

#!/usr/bin/env python

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import time

driver = webdriver.Chrome()

并且错误出现在最后一行:driver = webdriver.Chrome() 它是这样说的:

    Traceback (most recent call last):
  File "/Users/Edison/Desktop/untitled folder/huamai_jacket1.py", line 9, in <module>
    driver = webdriver.Chrome()
  File "/Users/Edison/anaconda2/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 61, in __init__
    log_path=service_log_path)
  File "/Users/Edison/anaconda2/lib/python2.7/site-packages/selenium/webdriver/chrome/service.py", line 42, in __init__
    start_error_message="Please see https://sites.google.com/a/chromium.org/chromedriver/home")
  File "/Users/Edison/anaconda2/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 42, in __init__
    self.port = utils.free_port()
  File "/Users/Edison/anaconda2/lib/python2.7/site-packages/selenium/webdriver/common/utils.py", line 36, in free_port
    free_socket.bind(('0.0.0.0', 0))
  File "/Users/Edison/anaconda2/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 49] Can't assign requested address
Exception AttributeError: "'Service' object has no attribute 'log_file'" in <bound method Service.__del__ of <selenium.webdriver.chrome.service.Service object at 0x1049a93d0>> ignored
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/Edison/Desktop/untitled folder/huamai_jacket1.py"]
[dir: /Users/Edison/Desktop/untitled folder]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

发生在今天早上,事情发生在这之后,有时当我访问甚至像 google.com 这样的网站时,ERR_ADDRESS_INVALID 页面经常出现,我需要不断刷新才能将页面返回到常规站点。

这是因为 driver.Chrome() 不包含 Chromedriver 位置的 PATH 吗?不过,我在今天前一周使用了我的脚本,一切都很完美。 请帮忙:(

【问题讨论】:

    标签: python google-chrome selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    因此,如果您没有将路径作为“webdriver.Chrome('path/to/chromedriver')”的一部分传递,则会沿着 PATH 环境变量搜索以找到它。所以我要做的第一件事就是验证我的 chromedriver 在哪里。如果它不在 $PATH 中,那么您可以将其放入 usr/bin 并尝试再次运行。或者,您可以将路径传递给 .Chrome() 中的 chromedriver 可执行文件。

    【讨论】:

    • 请问我能找到路径/to/chromedriver 吗?
    • 当然!它将是您将其下载到的位置。这将是一个名为“chomedriver”的文件。因此,在 Windows 上,您可以使用左下角的搜索。在 Mac 和 Linix 上,文件浏览器中有搜索功能。因此,搜索该文件,找到它的安装位置,然后复制该路径。
    猜你喜欢
    • 2016-08-28
    • 1970-01-01
    • 2018-12-01
    • 2022-12-01
    • 2021-09-29
    • 2022-06-16
    • 2016-06-12
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多