c#selenium浏览和上传文件

在我的selenium c#windows窗体应用程序中,我有一个上传doc / docx文件的场景。 我已经在另一个案例中上传了。因为它上面有一些我上传的浏览按钮的文本框

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

但现在在我目前的情况下,我无法使用此代码。 我的下图显示了上传文件对话框。 我google了很多,无法找到一个适合我的需求…我可以点击浏览按钮但不能选择一个文件。 Firebug和selenium IDE似乎没什么用处..

谁可以帮我这个事? 任何评论都会非常感激.. 在此处输入图像描述

由于它是一个操作系统对话框你无法用selenium处理它,你可以使用java脚本执行器检查这个链接Webdriver:File Upload

我也在寻找第三方图书馆,

除非没有任何其他窗口然后这对我有用:

  string url = "http://nervgh.github.io/pages/angular-file-upload/examples/image-preview/"; string path = @"C:\Users\File_Path"; IWebDriver d = new ChromeDriver(); d.Navigate().GoToUrl(url); d.FindElement(By.XPath("//input[@type='file']")).Click(); Thread.Sleep(5000); System.Windows.Forms.SendKeys.SendWait(path); System.Windows.Forms.SendKeys.SendWait(@"{Enter}"); 

或者只是为了最后一行

  System.Windows.Forms.SendKeys.SendWait("{Enter}"); 

在你的情况下,不要直接发送键到元素。 添加使用System.Windows.Forms的引用;

然后,

  d.FindElement(By.Id("uploadhere")).Click(); System.Windows.Forms.SendKeys.SendWait(@"C:\Some_Folder\MyFile.txt"); System.Windows.Forms.SendKeys.SendWait(@"{Enter}"); 

会工作。

我们无法通过Selenium处理windows fileOpenDialog。 我们必须使用Javascript执行程序,但有些时候仍然无法使用Javascriptexecutor。 它不会执行。 它给出了空值错误。 示例:

如果您通过以下链接访问naukri.com http://my.naukri.com/manager/createacc2.php?othersrcp=11499&wExp=N或

  

上传文件。

你的javascript和selenium不会识别浏览元素。

然后我们必须使用点位置等第三方工具来计算“ 浏览文件 ”按钮的x和Y坐标然后,我们可以使用C#.Net低级MouseClick手柄点击它或者我们可以使用AutoIt工具来处理wondows pop。 要了解更多信息,您可以访问

http://avinashpandeblogsonseleniumautomation.blogspot.in/2015/06/upload-file-using-selenium-web-driver.html您将通过示例获得解决方案。