【问题标题】:Selenium WebDriver and browsers select file dialogSelenium WebDriver 和浏览器选择文件对话框
【发布时间】:2012-02-09 16:26:08
【问题描述】:

我正在使用 selenium webdriver,C#。

是否可以使用 Firefox 选择文件对话框制作工作 webdriver? 还是我必须使用 AutoIt 之类的东西?

【问题讨论】:

    标签: c# selenium webdriver


    【解决方案1】:

    如果您尝试选择要上传的文件,Selenium 2 支持 HTML 文件输入。例如:

    HTML

    <input type="file" id="uploadhere" />
    

    硒代码

    IWebElement element = driver.FindElement(By.Id("uploadhere"));
    element.SendKeys("C:\\Some_Folder\\MyFile.txt");
    

    基本上你“输入”(使用SendKeys)文件输入元素的完整文件路径。 Selenium 为您处理文件选择对话框。

    但是,如果您想操作任意文件选择对话框,那么就像 Anders 所说,您必须在 Selenium 之外进行操作。

    【讨论】:

    • 这对我有用,即使网站有隐藏文件输入。
    • 我无法让它在 Selenium 2.48 中的 Firefox 上运行。
    • 这对我来说就像一个魅力!但是我怎样才能让它适用于多个文件呢?
    • @Jack - 也可以通过 SendKeys 上传多个文件 - 请参阅 stackoverflow.com/questions/23955430/…
    【解决方案2】:

    不,WebDriver 不能与对话框交互 - 这是因为对话框是操作系统的域,而不是网页。

    我知道有人对 autoit 以及 .Net 提供的自动化 API 很满意。

    另一种选择是完全跳过文件对话框并发出 POST 或 GET,但这需要更高级的网站知识以及如何构建 POST/GET。

    你可以试试Webinator,它类似于 Selenium,因为它是由 WebDriver 驱动的。它提供了文件对话功能,我已经取得了巨大的成功。

    【讨论】:

      【解决方案3】:

      这是另一个使用 remotewebdriver 的解决方案,它就像魔术一样工作,我喜欢它。

      这是我的课程:

      driver.findElementByLinkText("Upload Files").click();
      driver.setLogLevel(Level.ALL);
      System.out.println(driver.getCurrentUrl());
      WebElement element = driver.findElement(By.xpath("//input[@name='file_1']"));
      LocalFileDetector detector = new LocalFileDetector();
      
      //Now, give the file path and see the magic :)              
      String path = "D://test66T.txt";
      File f = detector.getLocalFile(path);
      ((RemoteWebElement)element).setFileDetector(detector);
      element.sendKeys(f.getAbsolutePath());
      
      //now click the button to finish
      driver.findElementByXPath("//html/body/div[9]/div[1]/a/span").click(); 
      

      【讨论】:

      【解决方案4】:

      您要求在文件对话框中使用 AutoIt。这很简单,您可以使用 C# 来完成。

      • 安装nuget包AutoItX.Net

      • 使用下面的演示代码

      • 根据需要更改对话框标题字符串

         public static void InsertIntoFileDialog(string file, int timeout = 10)
         {
             int aiDialogHandle = AutoItX.WinWaitActive("Save As", "", timeout); // adjust string as you need
             if (aiDialogHandle <= 0)
             {
                 Assert.Fail("Can't find file dialog.");
             }
             AutoItX.Send(file);
             Thread.Sleep(500);
             AutoItX.Send("{ENTER}");
             Thread.Sleep(500);
         }
        

      在我遇到与文件对话框相关的 Appium/Selenium 问题后,这对我有帮助。

      【讨论】:

      • 谢谢!我已经绞尽脑汁 1.5 天试图不使用另一个包。但我已经承认了,这真的很好用!对于阅读本文的任何人,它都会选择活动窗口,因此在调试时,请确保在 .Send() 之后添加断点,否则它会在 IDE 中键入文件路径。
      • 在我的例子中,该站点的文件输入被隐藏并且不接受击键。这个解决方案效果很好。
      【解决方案5】:

      According to Nadim Saker

      .Net 有一个库来处理文件上传对话框。它有一个 SendKeys 类,该类有一个方法 SendWait(string keys)。它在活动应用程序上发送给定的密钥并等待消息被处理。它不返回任何值。

      【讨论】:

        【解决方案6】:

        这可以通过以下方式完成,测试并使用 Internet Explorer 和 Chrome 驱动程序

        var allowsDetection = this.Driver as IAllowsFileDetection;
        if (allowsDetection != null)
        {
           allowsDetection.FileDetector = new LocalFileDetector();
        }
        
        Driver.FindElement(By.Id("your-upload-input")).SendKeys(@"C:\PathToYourFile");
        

        参考https://groups.google.com/forum/#!msg/webdriver/KxmRZ8MkM4M/45CT4ID_WjQJ

        【讨论】:

          【解决方案7】:

          如果您想上传文件而不使用 WebDriver,我遇到的唯一解决方案是 AutoIt。它允许您编写脚本并将其转换为可执行文件,然后您可以从代码中调用它。我在使用 ActiveX 控件时成功使用了它。

          【讨论】:

          • 使用 AutoIt 作为对问题的支持可能是一个很好的解决方法。还有一个 .NET 接口。因此,您可以在 C# 中轻松编写对话交互代码。 .....在这里您可以看到集成信息和将文本(例如路径)发送到窗口的示例:documentation.help/AutoItX/dotnet_usage.htm ..... AutiIt 也可作为 nuget 包使用。 (仅适用于测试 Windows 应用程序,不适用于移动设备。)
          • 我用 C# 编写了 AutoIt 的演示代码。它很快完成并且运行良好。看看我的回答:stackoverflow.com/a/64891474/789423
          【解决方案8】:

          另一种方法是使用System.Windows.Forms.SendKeys.SendWait("pathToFile")
          我在无法像@prestomanifesto 描述的那样向元素发送密钥的任何地方都成功使用它。

          【讨论】:

            【解决方案9】:

            我用这个来解决问题...如果以上都不起作用,请尝试

                Actions action = new Actions(driver);
                action.SendKeys(pObjElement, Keys.Space).Build().Perform();
            
                Thread.Sleep(TimeSpan.FromSeconds(2));
            
                var dialogHWnd = FindWindow(null, "Elegir archivos para cargar"); // Here goes the title of the dialog window
                var setFocus = SetForegroundWindow(dialogHWnd);
                if (setFocus)
                {
            
                    Thread.Sleep(TimeSpan.FromSeconds(2));
                    System.Windows.Forms.SendKeys.SendWait(pFile);
                    System.Windows.Forms.SendKeys.SendWait("{DOWN}");
                    System.Windows.Forms.SendKeys.SendWait("{TAB}");
                    System.Windows.Forms.SendKeys.SendWait("{TAB}");
                    System.Windows.Forms.SendKeys.SendWait("{ENTER}");
                }
            
                Thread.Sleep(TimeSpan.FromSeconds(2));
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2017-01-08
              • 1970-01-01
              • 1970-01-01
              • 2021-12-24
              • 2014-08-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多