【问题标题】:Download files with Selenium in Python在 Python 中使用 Selenium 下载文件
【发布时间】:2021-08-19 19:27:57
【问题描述】:

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

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

我收到以下消息:

selenium.common.exceptions.ElementClickInterceptedException:消息: 元素在点 (229,480) 处不可点击,因为另一个 元素遮住了它

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

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

【问题讨论】:

    标签: python selenium firefox web-scraping


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      终于,我可以做到了。我使用了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();')
      

      【讨论】:

        【解决方案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
        

        【讨论】:

          猜你喜欢
          • 2020-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-13
          • 2021-03-31
          • 1970-01-01
          相关资源
          最近更新 更多