【问题标题】:clicking on Save as option in internet explorer using python使用 python 在 Internet Explorer 中单击另存为选项
【发布时间】:2020-05-15 18:17:26
【问题描述】:

我正在尝试自动化下载一堆文件的过程。我正在使用 Internet Explorer 版本 11。(这是其他浏览器不兼容的唯一方法)。直到下载的部分是自动的,但是当点击下载链接时,IE会弹出一个窗口,显示天气打开,保存,另存为选项。


是否有任何 python 代码可以访问当前打开的 IE 并单击弹出窗口内的另存为按钮, 这是弹出窗口的快照 我必须单击附近的向下箭头才能保存 这是我每次自动化时都会导致错误的过程, 任何人都可以提供任何帮助


提前致谢

【问题讨论】:

  • 你怎么能点击保存。我有同样的问题。我无法点击打开。感谢您的帮助。

标签: python internet-explorer automation


【解决方案1】:

***编辑 找到了更好/更简单的方法。此外,我将调用方法包装在 try 语句中,以便您可以将其与其他代码一起使用,并且在按钮可用之前它不会尝试单击保存/保存。

点击保存按钮:

import uiautomation as uia

SaveBtn = uia.SplitButtonControl(SearchDepth=4, Name = 'Save')

#wrap up the Invoke statement to try until the box shows up
while True:
    try:
        SaveBtn.GetInvokePattern().Invoke()
    except LookupError:
        time.sleep(0.5)
    else:
        break

单击下拉菜单以单击另存为并保存到位置:

import uiautomation as uia

SaveLocation = "C:\\Foobar\\FoobarDrive\\FobarMagic\\""
savestr = "Foobar.xlsx
#Click the Saveas Button on Internet Explorer Window

#clicking the down arrow next to save
SaveBtn = uia.SplitButtonControl(SearchDepth=5, Name = '6')

    #wrap up the Invoke statement to try until the box shows up
while True:
    try:
        SaveBtn.GetInvokePattern().Invoke()
    except LookupError:
        time.sleep(0.5)
    else:
        break

    #Find/map saveas button and click the saveas option
SaveasBtn = uia.MenuItemControl(SearchDepth=2, Name = 'Save as')
SaveasBtn.GetInvokePattern().Invoke()

    #Find Save as dialog and click on address bar
Savedlg = uia.WindowControl(SearchDepth=1, Name = 'Save As')

    #Name File
FileName = Savedlg.EditControl(SearchDepth=7,Name='File name:')
FileName.SendKeys(savestr)

    #Click on the Previous locations button to activate the Addressbar
PrevLocBtn = Savedlg.ButtonControl(SearchDepth=7,Name = 'Previous Locations')
PrevLocBtn.GetInvokePattern().Invoke()

    #Find the Address bar and clear it out
Addressbar = Savedlg.EditControl(SearchDepth=8, Name = 'Address')
Addressbar.SendKeys('{Back}')

    #Define Save Location and Send the string to the addressbar
Addressbar.SendKeys(SaveLocation)

    #Click go to directory button
Navigatebtn = Savedlg.ButtonControl(SearchDepth=6, Name= 'Go to ' + '"' + SaveLocation + '"')
while True:
    try:
        Navigatebtn.GetInvokePattern().Invoke()
    except LookupError:
        time.sleep(0.5)
    else:
        break

#Click Saveas Dlg Save Button
SaveBtn = Savedlg.ButtonControl(SearchDepth=2, Name="Save")
SaveBtn.GetInvokePattern().Invoke()

#Overwrite the old file if it exists
confirm = Savedlg.WindowControl(SearchDepth=2, Name = 'Confirm Save As')
confirmbtn = confirm.ButtonControl(SearchDepth=2, Name = 'Yes')

while True:
    try:
        confirmbtn.GetInvokePattern().Invoke()
    except:
        break

如何打开文件

#Click the Save Button on Internet Explorer Window

    #clicking the save button itself.
OpenBtn = uia.SplitButtonControl(SearchDepth=4, Name = 'Open')

    #wrap up the click statement to try until the box shows up
while True:
    try:
        OpenBtn.GetInvokePattern().Invoke()
    except LookupError:
        time.sleep(0.5)
    else:
        break

【讨论】:

    【解决方案2】:

    据我所知,当您单击下载链接或按钮时,WebDriver 无法访问浏览器显示的 IE 下载对话框。但是,我们可以使用名为“wget”的单独程序绕过这些对话框。更多详情,您可以查看this article

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多