![](/img/trans.png)
[英]Handle Upload File Dialog using Selenium for a Material design application
[英]How to handle upload of file using Windows Upload Dialog in Selenium
我正在尝试使用Selenium(C#)上传附件。
在检查站点的DOM后,我注意到附加文件的链接使用的是object tags
。 以下是HTML摘录:
<object id="ctl00_mainContent_rauFilessilverlight03" class="ruObject" height="22px" type="application/x-silverlight-2" data="data:application/x-silverlight-2," style="width: 100%;">
<param value="/App/somelongjunkyparameters" name="source"/>
<param value="true" name="windowless"/> <param value="transparent" name="background"/>
<param value="some number" name="minRuntimeVersion"/>
<param value="PostData=anotherlongjunkyparameters,SilverlightRowId=ctl00_mainContent_rauFilessilverlight03,AsyncUploadId=ctl00_mainContent_rauFiles,MultipleSelection=Disabled,AllowedFileExtensions=,ServiceHandlerUrl=/App/Telerik.Web.UI.WebResource type=rau,MaxFileSize=0" name="InitParams"/>
<param value="true" name="autoUpgrade"/>
</object>
到目前为止,我已经尝试过了:
IWebElement fileAttachTA = driver.FindElement(By.XPath("//object[@class='ruObject']"));
fileAttachTA.Click();
String filePath = "C:/User/My Documents/file.txt";
Selenium能够找到对象,但是,我应该切换到Windows上载对话框吗? 希望能从任何有经验的人那里听到。
谢谢!
我明白了,我所做的就是:
IWebElement fileAttachTA = driver.FindElement(By.XPath("//object[@class='ruObject']"));
fileAttachTA.Click();
//Switch into the windows upload dialog
SendKeys.SendWait("^a");
Thread.Sleep(1000);
SendKeys.SendWait(file);
Thread.Sleep(1000);
SendKeys.SendWait(@"{Enter}");
Thread.Sleep(1000);
我使用System.Windows.Forms来使SendKeys.SendWait工作。 感谢大家!
无论开发网站的人是使用非标准机制上传文件。 查看您提供的HTML,它看起来像某种Silverlight控件。 尽管Selenium WebDriver可以在页面使用标准HTML上载机制(即<input type="file">
元素)时正确处理用于上载文件的文件选择对话框,但对于非标准上传机制。 您需要找到一种方法来处理Selenium之外的对话框。
下载/上传文件时,我在与Windows对话框交谈时遇到了问题。 我的解决方案是利用user32.dll GetForegroundWindow()。 然后根据标题文本创建了一些等待对话框消失的等待方法(仍然使用user32.dll)。 然后最终创建了一个对BeginInvoke的操作,等待窗口弹出,然后继续发送键。 现在没有我前面的代码示例,但是使用Google user32.dll Selenium,您会获得一些信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.