自动上传锚定标记的上传文件方案

时间:2019-05-13 12:38:39

标签: javascript java selenium

我正在尝试自动化行为,该行为具有与文件上载相关的多个场景,并带有不同的断言组合。 UI中有一个锚标记,而不是输入标记,这就是为什么我不能使用发送键上载文件的原因。我不想使用AutoIT,Sikuli之类的工具,因为它们与云环境中的远程执行不兼容,并增加了额外的库负担。

这是一个有角度的js应用程序,而DOM代码是

---“ a href =” javascript:void(0)“ title =”选择要上传的文件“ class =” btn btn-blue“ ng-show =” fileUploadSubmitted“ id =” file_input_btn“ ng-click = “ uploadFileAttachment()”

我试图找到:- 1:angular js库的任何内置上传功能,这样我就可以通过javascript执行程序执行它。 2:Sikuli和AutoIt正在工作,但这将是最后的解决方案 3:尝试编辑标签名称以在DOM中输入类型文件,后跟sendkey,但此方法不起作用

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法吗?

Tested locally not on the server.

# get the button element
ele = driver.find_element_by_id("file_input_btn")
# add a hidden file input ( might have to change the onchange event based on the events associated to the button in above line as you don't have a form)
driver.execute_script("var x=  document.createElement('INPUT');x.setAttribute('type', 'file'); x.setAttribute('onchange','this.form.submit()');x.setAttribute('hidden', 'true'); arguments[0].appendChild(x);",ele)
# send the file path here ( this should upload the file)
driver.find_element_by_xpath("//input[@type='file']").send_keys("file path should go here")