硒如何将文件上传到Microsoft Edge [英] Selenium how to upload files to Microsoft Edge
问题描述
我正在使用以下代码将文件上传到网站上的文件"类型元素.
I am using the following code to upload files to a website to a 'file' type element.
该代码在Firefox,Chrome和Safari中可以正常工作.
The code works fine in Firefox, Chrome and Safari.
但是,当我对Edge运行代码时,文件未上传
However when I run the code against Edge the file is NOT uploaded
driver.setFileDetector(new LocalFileDetector());
selectFile.sendKeys(path);
报告此错误: 该命令失败,因为指定的元素不可与指针或键盘交互.
This error is reported: The command failed because the specified element is not pointer or keyboard interactable.
如果我尝试这样使用Javascript:
If I try using Javascript like this:
document.getElementById('manual_file_selection').sendKeys(path)
我明白了:对象不支持属性或方法'sendKeys'
I get this: Object doesn't support property or method 'sendKeys'
如前所述,相同的代码在Chrome,Firefox和Safari中也能正常工作,所以我不理解.
As stated the same code works fine in Chrome, Firefox and Safari so I don't understand it.
这是文件上传按钮后面的代码:
This is the code behind the file upload button:
<div class="jsx-parser">
<div data-xxxxx-element="manual-file-selection">
<div class="button__container">
<label for="manual_file_selection" class="button button--primary" data-dragging="false" data-xxxxx-element="manual-file-selection--label">
<input id="manual_file_selection" type="file" accept="image/jpeg,image/png" data-xxxxx-element="manual-file-selection--input">
<span>Select File</span>
</label>
</div>
</div>
</div>
任何人都可以使用Selenium成功将文件上传到Edge还是不受支持?
Anyone had any success uploading files to Edge with Selenium or is it not supported?
推荐答案
根据您的错误消息,我会尝试一些Javascript.当我们执行JS来显示隐藏的input
元素,然后向其发送密钥时,这有点hacky,但是我过去曾经取得过成功.
Based on your error messages, I'd give some Javascript a try. It's a bit hacky, as we execute JS to reveal the hidden input
element, then send keys to it, but I've had success in the past.
// fetch the element
WebElement input = driver.findElement(By.XPath("//input[@type='file']"));
// run JS to reveal the element
JavascriptExecutor executor = (JavaScriptExecutor)driver;
executor.executeScript("arguments[0].style.display = 'block';", input);
// send file path keys
input.sendKeys(path);
值得一试.让我知道这是否有帮助.
It's worth a try. Let me know if this helps at all.
这篇关于硒如何将文件上传到Microsoft Edge的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!