使用ruby/Selenium上传文件 [英] Uploading files using ruby / Selenium
问题描述
我正在编写一个脚本,在其中单击按钮(选择照片")会打开一个文件上传(浏览器)框.我该如何设置我的文件名?
I am writing a script where I click on a button (Select Photo) which opens up a file upload (explorer) box. How am I to set my file name?
这是我用来单击选择照片"按钮(红宝石)的代码
This is the code I'm using to click on the Select Photo button (ruby)
driver.find_element(:id, "fileUploadButton").click
我看过一些帖子,说我不需要单击按钮,而是将路径发送到我要上传的文件/图像.那么我该如何在c:\ temp \ mypicture.jpg上上传文件?
I've seen some posts that says I do not need to click on the button but to send the path to the file/image I want to upload. So how would I upload a file on c:\temp\mypicture.jpg?
这是我完整而简单的代码.
Here's my full and simple code.
driver.navigate.to "http://blah blah" #the real site is an internal site
driver.find_element(:id, "claimGiftButtonDesktop").click
sleep 5
driver.find_element(:id, "fileUploadButton").click
单击fileUploadButton之后,将显示资源管理器窗口.如果我手动单击打开"或双击它,则会显示一个加载模式,并且图像显示在网站上.
After clicking the fileUploadButton, that's when the explorer window will show. If I manually click on Open or double click on it, then a loading modal shows and the image is shown on the website.
这是一条有效的IDE记录.我只是在将其转换为红宝石方面遇到问题.
Here is an IDE recording which works. I'm just having problems translating this into ruby.
open /PromoSite
click id=claimGiftButtonDesktop
click id=fileUploadButton
type id=fileInputElem #Value C:\\temp\\file.jpg
click id=viewProductPreviewButton
我还添加了一个屏幕截图.单击按钮,将显示文件上传".这应该很容易,所以我一定不要专注于正确的ID.
I've also added a screen shot. I click the button and the File Upload shows up. This should be something easy, so I must not be focusing on the correct id.
推荐答案
由于我没有您的代码,请假设我们正在测试https://encodable.com/uploaddemo/
As I have no your code, assume we are testing https://encodable.com/uploaddemo/
@driver.navigate.to "https://encodable.com/uploaddemo/"
element = @driver.find_element(:css, 'input[type=file]')
element.send_keys "/full/path/to/file.jpg"
@driver.find_element(:css, 'input[type=button]').click
因此,您应该将完整路径发送到输入字段,然后按提交"按钮
So, you should send the full path to the input field and press the "submit" button
这篇关于使用ruby/Selenium上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!