使用Cucumber JS将文件上传到Webdriver中的隐藏字段

问题描述 投票:-1回答:2

嗨,我需要使用cucumberJS和webdriverIO自动化网站。为此,我需要上传文件但该字段是隐藏的。例如 :

<input type="file" id='uploadFile' style="display: none"'>

但是webdriver无法识别UI上的元素。

提前致谢...

node.js selenium-webdriver webdriver cucumber cucumberjs
2个回答
1
投票

在webdriverIO v5中,通过使用本地文件路径作为参数调用type="file"将文件上载到.setValue()的输入。这似乎不适用于隐藏的输入,因为.setValue()首先调用.clearValue(),它会抛出Element could not be scrolled into view。要解决此问题,请直接在元素上调用.addValue()

input.addValue(filePath);

相关的API文档:https://webdriver.io/docs/api/element/addValue.html


0
投票

我得到了这个问题的解决方案。使用webdriverIO,我们可以执行javascript将样式显示从“none”更改为“block”。

client.execute(function() {
document.getElementById("element_id").style.display="block";
},function(err) {
client.uploadFile(localPath[,callback])    
if(err){
console.log("Error "+err);
}
});

然后将文件上传到该字段,然后再次将显示更改为无。

© www.soinside.com 2019 - 2024. All rights reserved.