在 Python 中使用 Selenium 下载文件

Download files with Selenium in Python

我正在尝试从包含阿根廷代表投票的 public 站点自动下载 excel 文件。例如,来自以下页面:https://votaciones.hcdn.gob.ar/votacion/4108

我正在使用 firefox webdriver 和 Selenium trhrough Python。 当我尝试单击图像中的按钮时:

我收到以下消息:

selenium.common.exceptions.ElementClickInterceptedException: Message: Element is not clickable at point (229,480) because another element obscures it

如果我尝试通过 driver.execute_script('javascript:exportExcel();') 执行下载 excel 页面内的特定脚本 没有任何反应。

有什么方法可以让它工作吗?

您需要单击下载按钮,然后使用 Autoit 或其他一些 ui 自动程序创建下载对话框的自动化。

终于,我可以完成了。我使用了 this page 中提出的例程。但是必须将 options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/vnd.ms-excel") 指令更改为 options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

工作示例是:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference("browser.download.folderList",2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir","/data")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

with webdriver.Firefox(options=options) as driver:
    
    driver.get('https://votaciones.hcdn.gob.ar/votacion/4108')
    driver.execute_script('javascript:exportExcel();')

这里是一个使用 Autoit 的例子。 Link: https://www.autoitscript.com/forum/topic/153520-iuiautomation-ms-framework-automate-chrome-ff-ie/

#include <GUIConstantsEx.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <EditConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIGdiDC.au3>
#include <WinAPIHObj.au3>
#include <WinAPISysWin.au3>
#include <AD.au3>
#include <Array.au3>
#include "UIAWrappers.au3"
#include <FileConstants.au3>
#include <WinAPIFiles.au3>
#include <GuiListBox.au3>
#include <Word.au3>
#include <File.au3>
#include <Excel.au3>


Opt("WinWaitDelay", 150)
Opt("WinTitleMatchMode", 2)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 2)
Opt("SendKeyDelay", 10)
Opt("GUIResizeMode", 1)
HotKeySet("^!x", "MyExit")

Global  $Web, $hWnd

$Web = "https://votaciones.hcdn.gob.ar/votacion/4108"

ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "--new-window --force-renderer-accessibility " & $Web, "", "")
WinWait("DSE - Voto Electrónico - Google Chrome", "", 8)
$hWnd = WinGetHandle("DSE - Voto Electrónico - Google Chrome")
WinActivate($hWnd)
WinSetState($hWnd, "", @SW_MAXIMIZE)
Sleep(7000); Give time for the webpage to load (out of country)

Local $oP6=_UIA_getObjectByFindAll($UIA_oDesktop, "Title:=DSE - Voto Electrónico - Google Chrome;controltype:=UIA_PaneControlTypeId;class:=Chrome_WidgetWin_1", $treescope_children)
_UIA_Action($oP6,"setfocus")
Local $oP5=_UIA_getObjectByFindAll($oP6, "Title:=DSE - Voto Electrónico;controltype:=UIA_DocumentControlTypeId;class:=Chrome_RenderWidgetHostHWND", $treescope_children)
_UIA_Action($oP5,"setfocus")

;~ First find the object in the parent before you can do something
Local $oUIElement=_UIA_getObjectByFindAll("XLSX.mainwindow", "title:=XLSX;ControlType:=UIA_HyperlinkControlTypeId", $treescope_subtree)
Local $oUIElement=_UIA_getObjectByFindAll($oP5, "title:=XLSX;ControlType:=UIA_HyperlinkControlTypeId", $treescope_subtree)
_UIA_Action($oUIElement, "setfocus")
_UIA_Action($oUIElement, "highlight")
_UIA_Action($oUIElement, "activate")
_UIA_Action($oUIElement, "leftclick")

Func MyExit()
    Exit
EndFunc   ;==>MyExit