【问题标题】:need help in solving selenium webdriver in python需要帮助解决 python 中的 selenium webdriver
【发布时间】:2020-08-01 15:11:23
【问题描述】:

我正在使用 PyCharm,并且正在关注 YouTube 教程。复制他们的方法后,我没有得到想要的结果。


from selenium import webdriver

class InstaBot:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://instagram.com")

InstaBot()
Traceback (most recent call last):
  File "/Users/prxthm/PycharmProjects/untitled1/bot.py", line 10, in <module>
    InstaBot()
  File "/Users/prxthm/PycharmProjects/untitled1/bot.py", line 7, in __init__
    self.driver = webdriver.Chrome()
AttributeError: 'function' object has no attribute 'Chrome'

【问题讨论】:

  • 你看到了什么错误?
  • 您必须添加完整路径,直到 chromedriver.exe。我相信您只是去了文件夹并忘记在最后附加文件名。
  • OK - 所以在您的最新代码中,没有定义到 chromedriver.exe 位置的路径。你把那个去掉了吗? - 运行代码时出现什么错误?
  • 我更新了错误,请问如何定义chromedriver.exe的路径
  • 您定义了以下答案中提到的路径...browser = webdriver.Chrome('/full/path/to/your/exe/chromedriver.exe') ...当您运行pip show selenium 时,输出是什么?最后,您的 chromedriver 版本是什么,它与您安装的 chrome 匹配吗?

标签: python-3.x selenium webdriver


【解决方案1】:

给出你的chromedriver以这种格式保存的路径,我已经输入了我保存webdriver.exe文件的路径:

driver = webdriver.Chrome(executable_path="C:\\Users\\dt\\workspace_python\\drivers\\chromedriver.exe")

尝试使用两个\\

【讨论】:

  • 我怎么忘了说我用的是mac,对不起。
【解决方案2】:

确保你已经安装了 selenium:

pip install selenium

确保您的路径是 chromedriver 的 .exe - 可能是其中之一:

browser = webdriver.Chrome('/Users/prxthm/Downloads/chromedriver.exe')
#or
browser = webdriver.Chrome('/Users/prxthm/Downloads/chromedriver/chromedriver.exe') 

如果这些都不起作用,请确保您分享您的错误。通常,错误是解决问题的最佳线索。

除了教程,阅读支持文档也很好。

  • 有关为 python 安装 selenium 的更多信息,请参阅here
  • 有关入门的更多信息,请参阅here

【讨论】:

  • 好的,我的代码出现了一个新错误,即 SyntaxError: EOL while scanning string literal
  • 哪一行? Eol 是行尾。您是否尝试过调查错误发生的原因?
  • 第二行末尾有一个流氓撇号。我已经更新了代码。是这样吗?
  • 它再次说'function'没有属性'chrome'
  • 您肯定已经通过 pip 安装了 selenium,并且您肯定获得了 chromedriver exe 的正确路径? .... 你是如何执行你的脚本的?
猜你喜欢
  • 1970-01-01
  • 2021-07-23
  • 2021-11-04
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
相关资源
最近更新 更多