如何在ruby中使用Robot类上传任何文件

dtcbnfnu  于 5个月前  发布在  Ruby
关注(0)|答案(1)|浏览(51)

enter image description here我需要自动附加文件按钮显示在图像上传文件.以前我已经做了这只是使用.Send_Keys("file_name"),但它不工作here.how我上传文件.pdf/.jpg在Ruby,这里是HTML代码-

<span class="trix-button-group trix-button-group--file-tools" data-trix-button-group="file-tools">
   <button type="button" class="trix-button trix-button--icon trix-button--icon-attach" data-trix-action="attachFiles" title="Attach Files" tabindex="-1">Attach Files</button>
   </span>

字符串
我试过的代码-

@driver.find_element(:css,"button.trix-button--icon-attach").click    
file = @driver.find_element(:id,"project_content")
file.send_keys("D:\download1.jpg")
sleep 5


有什么办法吗?我们可以在ruby中使用Robot类。
我在哪里可以找到类似于Java的Ruby方法/类?

mkh04yzy

mkh04yzy1#

使用file_field方法和set方法。

require 'watir'

# create a browser object
browser = Watir::Browser.new

# navigate to the web page with the file upload form
browser.goto("https://example.com/upload")

# find the file input element by name, id, or other attributes
file_input = browser.file_field(name: "file")

# set the file path to the file input element
file_input.set("C:\\files\\test.png")

字符串

相关问题