我决定从 rickroll 程序开始,只是为了好玩和学习基础知识。我从这里学到:https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 和这里:https://sites.google.com/a/chromium.org/chromedriver/getting-started 我以前用过 python,但几年没用了,我的代码在这里不起作用:
import os
import selenium
import time
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
webdriver.Chrome()
driver.get('https://www.youtube.com/')
time.sleep(5)
id_box = driver.find_element_by_id('search')
id_box.send_keys('Never gonna give you up')
time.sleep(5)
rickroll = driver.find_element_by_id('Rick Astley - Never Gonna Give You Up (Video)')
rickroll.click()
time.sleep(180)
driver.quit
我收到了这个错误信息:
Traceback (most recent call last):
File "C:\Users\ozfol\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 947, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1416, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ozfol\Desktop\Python files\rickroll.py", line 6, in <module>
driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
File "C:\Users\ozfol\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\ozfol\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
我将 chromedriver 添加到 PATH 但它仍然出现此错误
答案 0 :(得分:0)
如果您在 PATH 中有驱动程序的路径,为什么要将其作为参数提供?
也许使用 sys.environ['ENV_VARIABLE_NAME']
作为参数 executable_path
。