Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Selenium WebDriver和浏览器选择文件对话框_C#_Selenium_Webdriver - Fatal编程技术网

C# Selenium WebDriver和浏览器选择文件对话框

C# Selenium WebDriver和浏览器选择文件对话框,c#,selenium,webdriver,C#,Selenium,Webdriver,我使用的是SeleniumWebDriver,C 是否可以使用Firefox选择文件对话框使webdriver工作? 或者我必须使用AutoIt之类的工具吗?不,WebDriver不能与对话框交互-这是因为对话框是操作系统的域,而不是网页 我认识一些人,他们在autoit和.Net提供的自动化API方面都很幸运 另一种选择是完全跳过文件对话框并发布POST或GET,但这需要更深入的网站知识以及理解如何构建POST/GET 您可以尝试,它与Selenium类似,因为它是由WebDriver驱动的。

我使用的是SeleniumWebDriver,C

是否可以使用Firefox选择文件对话框使webdriver工作?
或者我必须使用AutoIt之类的工具吗?

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

我认识一些人,他们在autoit和.Net提供的自动化API方面都很幸运

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


您可以尝试,它与Selenium类似,因为它是由WebDriver驱动的。它提供了文件对话框功能,我在这方面取得了巨大的成功。

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

HTML

基本上,您可以使用SendKeys键入文件输入元素的完整文件路径。Selenium为您处理文件选择对话框


然而,如果你想操纵一个任意文件选择对话框,那么就像安德斯说的那样,你必须跳出Selenium。

这是另一个使用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(); 

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

另一种方法是使用System.Windows.Forms.SendKeys.SendWaitpathToFile。我在任何地方都成功地使用了它,我不能像@prestomanifesto所描述的那样只向元素发送键

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


这可以按如下方式完成,使用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");

参考

我用这个来解决这个问题。。。如果以上所有方法都不起作用,请尝试

    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));
}
您要求在文件对话框中使用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问题后,这对我很有帮助。

这非常有效,省去了在测试箱(如AutoIT等)上安装额外软件的巨大麻烦。不过,这是Java绑定吗?因为在C语言中,我在LocalFileDetector下找不到getLocalFile方法,即使站点有隐藏的文件输入,这对我来说也是有效的。我无法在Selenium 2.48中的Firefox上使用它。这对我来说就像一个符咒!但是我如何才能使它适用于多个文件?@Jack-也可以通过SendKeys上载多个文件-请参阅使用AutoIt作为问题支持是一个很好的解决方法。还有一个.NET接口。所以你可以用C写对话交互的代码。。。。。在这里,您可以看到集成信息和发送文本的示例,例如指向窗口的路径:。。。。。AutiIt也可作为nuget软件包提供。只对测试Windows应用程序有用,不适用于手机。我用C语言为AutoIt编写了一个演示代码。它完成得很快,效果也很好。看看我的答案:谢谢!我绞尽脑汁想了1.5天,尽量不使用其他软件包。但是我承认了,这真的很好!对于阅读本文的任何人来说,它都会选择活动窗口,因此在调试时,请确保在发送后添加断点。否则,它会在IDE中键入文件路径。
    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));
}
 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);
 }