如何使用Selenium Web驱动程序上传文件/句柄窗口

我正在尝试自动化我必须上传文档的场景,但是当我单击Web应用程序上的“选择文件”按钮后,我无法处理“ 文件上载”窗口。

我正在使用C#语言和Chrome浏览器。

我在C#中的测试用例:

[Test, Description("JAVA_67350: Filename is shown as tooltip in browse button after upload is cancelled in Firefox browser ")] [Component(Component.UploadBox)] [Priority(Priority.High)] [CustomerID(160100)] public void JAVA_67350() { driver.Navigate().GoToUrl("http://localhost/JavaScript/CR_Samples/JavaScriptSamples/JAVA_67350/UploadBoxUntitled.html"); driver.Manage().Window.Maximize(); Thread.Sleep(3000); //clicking upload button driver.FindElement(By.XPath(".//*[@id='UploadDefault']/div[1]/input[2]")).Click(); Thread.Sleep(TimeSpan.FromSeconds(1)); //file uploading SendKeys.SendWait("C:\\UploadBox\\Examplefordocx.docx" + @"{RIGHT}"); Thread.Sleep(500); SendKeys.SendWait(@"{TAB}"); Thread.Sleep(500); SendKeys.SendWait(@"{TAB}"); Thread.Sleep(500); SendKeys.SendWait(@"{ENTER}"); Thread.Sleep(3000); } 

如果您的File元素具有'input type = file'则可以尝试以下'input type = file'element(by.css('input[type="file"]')).sendKeys(pathToFile);

这不会打开文件上载窗口对话框。 相反,这会将所需的值发送到输入文件字段。

有关详细信息,请参阅以下链接: http : //seleniumeasy.com/selenium-tutorials/uploading-file-with-selenium-webdriver

如何使用Java中的Selenium WebDriver上传文件

问候,
Sakshi

你可以使用SendKeys

 using System.Windows.Forms; SendKeys.SendWait(@"C:\temp\file.txt"); SendKeys.SendWait(@"{Enter}"); 

Selenium没有任何内置函数来处理文件上传/与windows对话(打印对话)/ windows弹出窗口交互。

如果要与Windows对象进行交互,可以使用AutoIT

它是与windows对象交互并且非常易于使用的绝佳工具。 有关使用的步骤,请参阅以下链接。

http://www.toolsqa.com/seleniu-webdriver/autoit-selenium-webdriver/