Rubyのロボットクラスを使用してファイルをアップロードする方法
概要
ここに画像の説明を入力してください ファイルをアップロードするには、画像に示されている「ファイルを添付」ボタンを自動化する必要があります。以前は、 .Send_Keys(“file_name”) を使用してこれを実行しましたが、ここでは機能しません。ファイル .pdf/.jpgn 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 のメソッド/クラスはどこで見つけられますか?
解決策
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")