使用Python中的Selenium下载文件

2024-09-27 22:26:48 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我正在通过Python使用firefox webdriver和Selenium。 当我尝试单击图像中的按钮时: enter image description here

我收到以下信息:

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

如果我试图通过driver.execute_script('javascript:exportExcel();')在页面内执行特定于下载excel的脚本 什么也没发生

有什么办法可以让它工作吗


Tags: 文件https网站selenium代表页面firefox投票
3条回答

下面是一个使用Autoit的示例。链接: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

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

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

例如:

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();')

相关问题 更多 >

    热门问题