简体   繁体   English

如何使用Selenium中的Windows上载对话框处理文件上载

[英]How to handle upload of file using Windows Upload Dialog in Selenium

I am trying to upload an attachment using Selenium (C#). 我正在尝试使用Selenium(C#)上传附件。

Upon checking the DOM of the site, I noticed that the link to attach files is using object tags . 在检查站点的DOM后,我注意到附加文件的链接使用的是object tags Below is the HTML excerpt: 以下是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>

I have tried this so far: 到目前为止,我已经尝试过了:

IWebElement fileAttachTA = driver.FindElement(By.XPath("//object[@class='ruObject']"));
fileAttachTA.Click();
String filePath = "C:/User/My Documents/file.txt";

Selenium was able to find the object, but, should I switch to the Windows Upload Dialog? Selenium能够找到对象,但是,我应该切换到Windows上载对话框吗? Hoping to hear from anyone who has experience in this. 希望能从任何有经验的人那里听到。

Thanks! 谢谢!

I got it and what I did was this: 我明白了,我所做的就是:

 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);

I used the System.Windows.Forms to get the SendKeys.SendWait to work. 我使用System.Windows.Forms来使SendKeys.SendWait工作。 Thanks everyone! 感谢大家!

Whoever developed the website is using a non-standard mechanism for uploading files. 无论开发网站的人是使用非标准机制上传文件。 Looking at the HTML you provided, it looks like a Silverlight control of some sort. 查看您提供的HTML,它看起来像某种Silverlight控件。 While Selenium WebDriver can properly handle the file selection dialog for uploading a file when a page is using a standard HTML upload mechanism (ie, an <input type="file"> element), it has no hope of doing so with a non-standard upload mechanism. 尽管Selenium WebDriver可以在页面使用标准HTML上载机制(即<input type="file">元素)时正确处理用于上载文件的文件选择对话框,但对于非标准上传机制。 You'll need to find a way to handle the dialog outside of Selenium. 您需要找到一种方法来处理Selenium之外的对话框。

I've had issues with talking to a Windows dialog box when downloading/uploading files. 下载/上传文件时,我在与Windows对话框交谈时遇到了问题。 My solution was to utilize user32.dll GetForegroundWindow(). 我的解决方案是利用user32.dll GetForegroundWindow()。 Then created some wait methods for the dialogue box to appear disappear based on header text(still using user32.dll). 然后根据标题文本创建了一些等待对话框消失的等待方法(仍然使用user32.dll)。 Then finally created an action to BeginInvoke, waited for the window to pop up, and proceeded with send keys. 然后最终创建了一个对BeginInvoke的操作,等待窗口弹出,然后继续发送键。 Don't have code examples in front of me right now, but Google user32.dll Selenium and you'll get some info. 现在没有我前面的代码示例,但是使用Google user32.dll Selenium,您会获得一些信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM