How to handle File download dialog/popup in IE browser USING SELENIUM and C#

时间:2015-07-31 19:59:10

标签: file internet-explorer selenium download

I have page which downloads a file which shows a dialog on the bottom with OPEN SAVE CANCEL options, how can I click those options ? I am using IE browser, I saw some solutions using third party AutoIt, Robot class, but I am looking with Selenium and C# only. Attached is the image of what I am talking.. Any idea how can we do this ?enter image description here

5 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码。

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;    
using OpenQA.Selenium.Support.UI;
using System.Threading;
using System.Collections.Generic;
using System.Windows.Forms;
//using NUnit.Framework;

namespace SampleTest
{
    [TestMethod]
    public void Download()
    {
        IWebDriver driver = new InternetExplorerDriver(@"C:\Users\hamit\Desktop\Selenium\IEDriverServer_Win32_2.48.0");
        driver.Navigate().GoToUrl("https://www.spotify.com/se/download/windows/");

        Thread.Sleep(2000);
        SendKeys.SendWait("@{TAB}"); Thread.Sleep(100);
        SendKeys.SendWait("@{TAB}"); Thread.Sleep(100);
        SendKeys.SendWait("@{DOWN}"); Thread.Sleep(100);
        SendKeys.SendWait("@{DOWN}"); Thread.Sleep(100);
        SendKeys.SendWait("@{Enter}");
    }
}

答案 1 :(得分:0)

就像一个选项-尝试获取所有存在的按钮,然后按内部文本进行过滤

var posibleButtons = driver.FindElements(By.TagName("button")).Where(el => el.Text.Contains("Open"));
posibleButtons.Where(// try use some other filters, maybe by styles or ets...

答案 2 :(得分:0)

出于安全原因,IE 11 不允许我们跳过该部分。由于这不是 DOM,因此您无法使用 selenium 处理它..

AutoIT 或类似工具是唯一的选择,这会降低测试的可靠性..

相反,我们添加了 API 调用来获取文档,而不是实际下载文档。

答案 3 :(得分:0)

在 Java 中尝试过,文件已下载。

driver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "j"));
Thread.sleep(2000);
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_ENTER);

说明:

1 首先点击按钮/链接下载文件。会看到这个弹出窗口 在此处输入图片描述 enter image description here 2. 单击“Ctrl+j”这将打开“查看下载弹出窗口”。 3.然后点击回车,因为焦点会在最近的文件上,所以 将被下载

答案 4 :(得分:-1)

 AutoItX3 autoit = new AutoItX3();
                autoit.WinActivate("Save");            
                Thread.Sleep(1000);
                autoit.Send("{F6}");
                Thread.Sleep(1000);
                autoit.Send("{TAB}");
                Thread.Sleep(1000);
                autoit.Send("{ENTER}");