简体   繁体   English

如何在没有“输入”元素的情况下在 Selenium WebDriver 中上传文件

[英]How to upload a file in Selenium WebDriver with no 'input' element

I have a HTML page with button named "Upload" and id: btn-import-questions .我有一个 HTML 页面,带有名为“上传”和 id: btn-import-questions按钮。 The element:元素:

<button class="btn btn-success btn-sm col-lg-11" id="btn-import-questions" data-ts-file-selector="questions-import-init">  Upload&nbsp;<i class="fa fa-upload"></i></button>

I tried a Selenium Java code like this:我尝试了这样的 Selenium Java 代码:

driver.findElement(By.id("btn-import-questions")).sendkeys("C:/path/to/file.xlsx");

But since this is an upload button and not an input-type element, the above code is not working.但由于这是一个上传按钮而不是输入类型元素,所以上面的代码不起作用。

Check the DOM because somewhere there must be an <input type="file"> .检查 DOM,因为某处必须有一个<input type="file"> The website's javascript will call the .click() of this element to pop up the file selector dialog and closing the dialog with a selection will provide the path.网站的 javascript 将调用此元素的 .click() 以弹出文件选择器对话框,并通过选择关闭对话框将提供路径。 With Selenium the same can be achieved with the .sendkeys():使用 Selenium 可以通过 .sendkeys() 实现相同的效果:

driver.findElement(By.xpath("//input[@type=\"file\"]")).sendkeys(localFilePath);

You are doing it almost correctly, except that sendKeys() should be called on the input with type="file" that is, most likely invisible in your case.您几乎正确地执行了此操作,除了应在type="file"的输入上调用sendKeys() ,也就是说,在您的情况下很可能是不可见的。 If this is the case, make the element visible first:如果是这种情况,请先使元素可见:

This one works for me:这个对我有用:

    String CSVFile = "C:\\D\\Projects\\file.csv";
WebElement fileElement=this.driver.findElement(By.xpath("//[text()='fileElement']"));
            this.wait.until(ExpectedConditions.elementToBeClickable(fileElement ));
            fileElement .click();

            StringSelection ss = new StringSelection(CSVFile);
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

            //native key strokes for CTRL, V and ENTER keys
            Robot robot = new Robot();

            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);

You can use AutoIT tool to do this.您可以使用 AutoIT 工具来执行此操作。 Use below code in AutoIT .Au3 file to upload.在 AutoIT .Au3 文件中使用以下代码进行上传。

sleep(1000)
If WinExists("[TITLE:Open]") Then

 Local $hWnd = WinWaitActive ("[TITLE:Open]", "",15)
 WinActivate($hWnd)
 ;WinWaitActive("Open", "", 10)
 ControlFocus("Open","","Edit1")
 ControlsetText("Open","","Edit1",$CmdLine[1])
 ControlClick("Open","","Button1")

ElseIf WinExists("[TITLE:File Upload]") Then

 Local $hWnd = WinWaitActive ("[TITLE:File Upload]", "",15)
 WinActivate($hWnd)
 ;WinWaitActive("Open", "", 10)
 ControlFocus("File Upload","","Edit1")
 ControlsetText("File Upload","","Edit1",$CmdLine[1])
 ControlClick("File Upload","","Button1")

Else

 Local $hWnd = WinWaitActive ("[TITLE:Choose File to Upload]", "",15)
 WinActivate($hWnd)
 ;WinWaitActive("Open", "", 10)
 ControlFocus("Choose File to Upload","","Edit1")
 ControlsetText("Choose File to Upload","","Edit1",$CmdLine[1])
 ControlClick("Choose File to Upload","","Button1")

EndIf

And then use below code in you C# code to invoke it.然后在你的 C# 代码中使用下面的代码来调用它。

String sExe=(<EXE file path>+" "+<Upload file path>);

Runtime.getRuntime().exec(sExe);
Thread.sleep(5000);

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

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