简体   繁体   English

在 Windows 弹出窗口中使用 Selenium 上传文件

[英]Upload Documents using Selenium in Windows pop up

I'm trying to upload documents in Selenium using C# on windows bas pop up using Sendkeys.Sendwait(filepath) however I have connected to RDC once it get locked automatically SendKeys.Sendwait won't work..is there any alternative I can use to upload file on Windows using Selenium我正在尝试在 Selenium 中使用 C# 在 windows 上使用 Sendkeys.Sendwait(filepath) 弹出来上传文档但是我已经连接到 RDC 一旦它自动锁定 SendKeys.Sendwait 将无法工作..有没有我可以使用的替代方法使用 Selenium 在 Windows 上传文件

Selenium using C# up loading documents on Windows pop upI cannot use Xpath/ID Selenium using C# up loading documents on Windows pop upI cannot use Xpath/ID

What I did was use WindowsForms to be able to sendkeys to the upload window. If you have a console app, this approach may not work for you.我所做的是使用 WindowsForms 将密钥发送到上传 window。如果你有一个控制台应用程序,这种方法可能不适合你。 It essentially looks something like this:它基本上看起来像这样:

using System.Windows.Forms;

//This button click opens the Windows Upload Window
driver.FindElement(By.ClassName("UploadButton")).Click();

//The thread sleep is to account for the upload window opening
Thread.Sleep(2000);

//In the SendKeys, we send the full path to what we want to upload, as well as {RIGHT} 
//which represents clicking right on the key board which will move the focused option in 
//the window to the "Enter" option
SendKeys.SendWait(GetProjectRoot() + @"\DataFiles\red.png" + @"{RIGHT}");

//This Send Keys presses the enter key, submitting the path we inputted for upload
SendKeys.SendWait(@"{Enter}");

//Accounts for the upload + window closing
Thread.Sleep(2000);

The only downside to this is that if you click somewhere else on windows when it comes to send the path, it will send the path on whichever other window you clicked.唯一的缺点是,如果您在发送路径时单击 windows 上的其他位置,它将发送您单击的任何其他 window 上的路径。

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

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