如何使用javascript - Selenium提交文件输入?



>我在我的应用程序中使用C#硒,我单击文件输入并显示(打开窗口(文件对话框。我需要传递文件路径,然后按回车(返回(键。

要求:即使当前用户已注销、桌面已锁定、RDP 会话已关闭,也可以正常工作

  • Selenium内置的SendKeys根本不起作用。
  • Windows.Forms.SendKeys.SendWait(( 工作不是那么完全,路径提交是 好的,为了按回车键,进程等待用户会话。努格特 -
  • InputSimulator/WinApi - UAC (UIPI( 访问被拒绝

是否有可能在不打开javascript对话框中的情况下提交文件?

谢谢

使用硒的例子

var button = web.FindElementByXPath("//span[@class='upload']");
button.Click();

不工作

button.SendKeys("path"); //not input element
Windows.Forms.SendKeys.SendWait("..."); //not working for closed user session
InputSimulator.SimulateTextEntry("path"); //access denied because of Windows UIPI

在上面解释的情况下,没有文件类型的HTML输入(请参阅注释(。单击其他HTML元素后打开文件对话框弹出窗口。将任何字符串传递给该元素都没有成功。

我在 c# 应用程序中使用的最好也可能是最简单的解决方案是使用 AutoIT。

Install-Package AutoItX.Dotnet

单击后呼叫以打开对话框

AutoItX.ControlSend("Open", "", "", _sysPathToTheFile);
AutoItX.ControlSend("Open", "", "", "{ENTER}");

或者,您可以调用

WinActivate(), WinWaitActive()...

但是当窗口在后台或在我的情况下,使用会话关闭、锁定屏幕和类似情况时,ControlSend(( 也能很好地工作。

如果在 dom 中的某个地方有一个文件输入 (<input type='file'/>(,你可以做这样的事情:

https://www.selenium.dev/documentation/webdriver/remote_webdriver/#local-file-detector

IWebElement upload = driver.FindElement(/*select the <input> element*/);
upload.SendKeys(filepath); // as string

最新更新