简体   繁体   English

Python,网络驱动程序错误(Selenium)

[英]Python, error with web driver (Selenium)

    import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get('http://arithmetic.zetamac.com/game?key=a7220a92')
element = driver.find_element_by_link_text('problem')
print(element)

I am getting the error:我收到错误:

FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'

I am not sure whythis is happening, because I imported selenium already.我不确定为什么会发生这种情况,因为我已经导入了硒。

Either you provide the ChromeDriver path in webdriver.Chrome or provide the path variable您要么在 webdriver.Chrome 中提供 ChromeDriver 路径,要么提供路径变量

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driverLocation = 'D:\Drivers\chromedriver.exe' #if windows
driver = webdriver.Chrome(driverLocation) 
driver.get('http://arithmetic.zetamac.com/game?key=a7220a92')
element = driver.find_element_by_link_text('problem')
print(element)

Best way to eliminate this Exception without altering the code eevn single line is to add the chromedriver.exe( or nay other browser driver files) in to Python在不改变单行代码的情况下消除此异常的最佳方法是将 chromedriver.exe(或其他浏览器驱动程序文件)添加到 Python 中

  1. site_packages/scripts directory for windows windows 的 site_packages/scripts 目录
  2. dist_package/scripts for Linux Linux 的 dist_package/scripts

Please check this solution, it works.请检查此解决方案,它有效。

If you are using a Mac, then don't include '.exe' I put the selenium package directly into my Pycharm project that I called 'SpeechRecognition'.如果您使用的是 Mac,则不要包含“.exe”,我将 selenium 包直接放入我称为“SpeechRecognition”的 Pycharm 项目中。 Then in the selenium file, navigate to: /selenium/webdriver/chrome, then copy and paste the 'chromedriver.exe' file you downloaded most likely from [here][1] Try this script if you are using PyCharm IDE or similar.然后在 selenium 文件中,导航到:/selenium/webdriver/chrome,然后复制并粘贴您最有可能从 [此处][1] 下载的“chromedriver.exe”文件。如果您使用的是 PyCharm IDE 或类似软件,请尝试使用此脚本。 This should open a new Google window for you.这应该会为您打开一个新的 Google 窗口。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome('/Users/Name/PycharmProjects/SpeechRecognition/selenium/webdriver/chrome/chromedriver')
browser.get('http://www.google.com')

Then if you want to automatically search an item on Google, add these lines below and run.然后如果你想在谷歌上自动搜索一个项目,在下面添加这些行并运行。 You should see an automatic google search window opening up.您应该会看到一个自动 google 搜索窗口打开。 It might disappear quickly but to stop that, you can simply add a while loop if you want or a timer它可能会很快消失,但要阻止它,您可以根据需要简单地添加一个 while 循环或一个计时器

search = browser.find_element_by_name('q')
search.send_keys('How do I search an item on Google?')
search.send_keys(Keys.RETURN)


  [1]: https://sites.google.com/a/chromium.org/chromedriver/home

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM