如何使用RSelenium通过浏览器对话框完成文件上传操作
问题原因
你当前操作的id=showrecfile是用于触发系统文件选择框的前端按钮,Selenium无法直接与操作系统的文件选择弹窗交互,你需要将文件路径直接发送给页面中隐藏的<input type="file">元素完成上传。
修改后的代码
受体文件上传部分替换为如下代码:
# 点击唤起上传区域(保证文件输入元素正常渲染) receptor_trigger <- browser$findElement(using = "id", "showrecfile") receptor_trigger$clickElement() # 等待文件输入元素加载,可根据页面实际加载速度调整等待时长 Sys.sleep(1) # 定位真正的文件上传输入元素,Cluspro表单中受体文件input的name属性为recfile receptor_input <- browser$findElement(using = "css selector", "input[name='recfile']") # 直接发送本地文件绝对路径,Mac系统下直接使用标准unix路径即可,无需额外转义 receptor_input$sendKeysToElement(list(normalizePath(receptor_pdb)))
配体文件上传部分同理调整:
ligand_trigger <- browser$findElement(using = "id", "showligfile") ligand_trigger$clickElement() Sys.sleep(1) ligand_input <- browser$findElement(using = "css selector", "input[name='ligfile']") ligand_input$sendKeysToElement(list(normalizePath(ligand_pdb)))
注意事项
- 提前用
file.exists(receptor_pdb)在R环境中验证文件路径有效性,确保使用的是正确的绝对路径 - 若遇到元素不可见报错,可执行如下代码强制显示文件输入元素后再传路径:
browser$executeScript("document.querySelector('input[name=recfile]').style.display = 'block';", args = list())
内容的提问来源于stack exchange,提问作者littleworth
相关产品推荐
相关产品推荐

