单击带有 Python 的按钮脚本

Click button script with Python

尝试 运行 以下脚本转到 this 网站并单击 link 导出 csv。

from selenium import webdriver
driver=webdriver.Firefox()
driver.get("https://www.draftkings.com/contest/gamecenter/46877680")
elem1 = driver.find_element_by_link_text("Export Lineups to CSV")
elem1.click()

我收到以下错误并且找不到 geckodriver。我 pip 安装了 selenium。我从一个较旧的视频中得到了这个,我目前 运行 Python 3.6 所以这可能也是问题的一部分。我哪里错了?

Traceback (most recent call last): File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start stdout=self.log_file, stderr=self.log_file) File "C:\Program Files\Python36\lib\subprocess.py", line 707, in init restore_signals, start_new_session) File "C:\Program Files\Python36\lib\subprocess.py", line 992, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified

在处理上述异常的过程中,又发生了一个异常:

Traceback (most recent call last): File "C:/Users/mike/Desktop/Lineup1.py", line 2, in driver=webdriver.Firefox() File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 144, in init self.service.start() File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

实际上,Selenium 客户端绑定尝试从系统 PATH 中找到 geckodriver 可执行文件。您需要将包含可执行文件的目录添加到系统路径。 在 Unix 系统上,如果您使用 bash-compatible shell:

,您可以执行以下操作将其附加到系统的搜索路径中
export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step

在Windows,您需要更新Path系统变量以手动或命令行将完整目录路径添加到可执行的geckodriver(不要忘记在将可执行的geckodriver添加到系统后重新启动系统PATH 生效)。原理和Unix一样。

然后尝试使用以下

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)

参考,

https://github.com/mozilla/geckodriver/releases

https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver

下载 geckodriver 并将 geckodriver.exe 文件拖放到项目根文件夹。这样就不需要在你的代码中添加geckodriver路径再尝试执行代码。不会出现错误