Selenium和Python以及使用Chrome驱动程序的错误

时间:2019-06-23 20:58:49

标签: python selenium-webdriver selenium-chromedriver

编辑:代码在第一行之后失败。我已经重新安装了Google Chrome,重新安装了webdriver和selenium,但是我似乎无法解决问题。屏幕截图已随附。我也是Image here! MacOS。 感谢@Dimitri T和@Omer Tekbiyik到目前为止的帮助!

我正在尝试通过chromedriver使用selenium和python,但似乎无法克服一系列错误。我几乎对所有东西都进行了故障排除。任何帮助将不胜感激!


from selenium import webdriver
# os.environ["webdriver.chrome.driver"] = chromedriver
# browser = webdriver.Chrome(chromedriver)
# browser.get("https://newclasses.nyu.edu/portal/site/a3aa9fb7-82a4-4b7e-# ac96-2e50b60cbbbc/tool/b81f9600-6b1e-452f-9e1a-ea4af0d2fb4a/main")

# title = browser.title
# print(title)

这是我尝试过的代码。 我也尝试过这个...

from selenium import webdriver

# browser = webdriver.Chrome()
# browser.get("https://newclasses.nyu.edu/portal/site/a3aa9fb7-82a4-4b7e-# # ac96-2e50b60cbbbc/tool/b81f9600-6b1e-452f-9e1a-ea4af0d2fb4a/main")

# title = browser.title
# print(title)

我希望它能启动一个网页,但是,我只是收到一连串错误。

Traceback (most recent call last):
  File "/Users/trapbookpro/Downloads/PythonLoginScripts/yes.py", line 1, in <module>
    from selenium import webdriver
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/__init__.py", line 18, in <module>
    from .firefox.webdriver import WebDriver as Firefox  # noqa
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 29, in <module>
    from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 21, in <module>
    import copy
  File "/Users/trapbookpro/Downloads/PythonLoginScripts/copy.py", line 10, in <module>
    lst1()

3 个答案:

答案 0 :(得分:0)

您只需要添加驱动程序路径,如:

driver_path = r'your driver path'
browser = webdriver.Chrome(executable_path=driver_path)

并获得像这样的标题:

from selenium import webdriver

driver_path = r'your path'
browser = webdriver.Chrome(executable_path=driver_path)
browser.get("https://newclasses.nyu.edu/portal/site/a3aa9fb7-82a4-4b7e-ac96-2e50b60cbbbc/tool/b81f9600-6b1e-452f-9e1a-ea4af0d2fb4a/main")
title = browser.title
print(title)

输出:

NYU Login

答案 1 :(得分:0)

  1. 让我们开始清理。使用Selenium package安装PIP

    pip install -U selenium
    
  2. 下载并安装Chrome
  3. 下载Chromedriver(确保为您的Chrome浏览器版本选择匹配的版本)
  4. 修改您的代码,使其看起来像:

    from selenium import webdriver
    
    browser = webdriver.Chrome("c:\\path\\to\\chromedriver.exe")
    browser.get(
        "https://newclasses.nyu.edu/portal/site/a3aa9fb7-82a4-4b7e-ac96-2e50b60cbbbc/tool/b81f9600-6b1e-452f-9e1a-ea4af0d2fb4a/main")
    title = browser.title
    print(title)
    browser.quit()
    
  5. 就是这样,您的脚本现在应该可以运行了:

    enter image description here

更多信息,包括安装,配置步骤和示例项目:Selenium With Python

答案 2 :(得分:0)

我找到了解决此问题的方法!在尝试了Jupyter之后,我找到了一种使此脚本起作用的方法!

sudo -H pip3 install -U selenium
sudo -H pip3 install urllib3
sudo easy_install selenium 

这很完美,并且在python3中运行我的脚本也使我能够成功运行此脚本。谢谢大家,一切!