【问题标题】:Unable to upload a file with Selenium - Ruby无法使用 Selenium 上传文件 - Ruby
【发布时间】:2018-05-04 06:59:13
【问题描述】:

我正在尝试使用用 Ruby 编写的机器人上传文件。它失败了,但我认为我的合成器很好。我想最近升级的 Firefox 让我无法完成这项工作。请问你有什么想法吗?

我使用这些资源来编写我的代码:

https://saucelabs.com/resources/articles/best-practices-tips-selenium-file-upload

http://elementalselenium.com/tips/1-upload-a-file

我在这里得到了最后一个版本的 selenium:

https://rubygems.org/gems/selenium-webdriver/versions/2.53.0?locale=fr

我在这里得到了最新版本的 geckodriver:

https://rubygems.org/gems/geckodriver-helper/versions/0.0.3

但它不起作用:WebDriverError@chrome://marionnette/content/error.js:235:5 找不到文件

我在本地模式下使用 wordpress 和 local by flywheel。 火狐量子57.0 64位

这是我的小代码:

require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers

def setup
  @driver = Selenium::WebDriver.for :firefox
end

def teardown
  @driver.quit
end

def run
  setup
  yield
  teardown
end

run do
  @driver.get "http://mywebsite.dev/wp-admin/profile.php"
  inputlogin = @driver.find_element :id => "user_login"
  inputpwd = @driver.find_element :id => "user_pass"

  inputlogin.send_keys "mylogin"
  inputpwd.send_keys "mypwd"
  @driver.find_element(:id, "wp-submit").click

  @driver.get "http://mywebsite.dev"
  element = @driver.find_element(:id, 'uploadInput1')
  element.send_keys "C:\\path\\image.jpg"

  # @driver.find_element(id: 'btn-submit').click

  puts "Successful on #{@driver.title}"
end

由于它不起作用,我正在寻找另一种上传文件的方法,因此我尝试使用 AutoIT,但失败了……这是一个示例。

http://www.maisasolutions.com/blog/How-to-upload-images-using-selenium-webdriver-with-ruby

此外,此解决方案非常繁重(下载、安装、不可复制等),并且似乎不是正确的代码。

感谢您的帮助。

【问题讨论】:

    标签: ruby wordpress selenium file-upload upload


    【解决方案1】:

    在页面上找到一个文件字段并附加一个给定路径的文件。 文件字段可以通过其名称、ID 或标签文本找到。

    page.attach_file(locator, '/path/to/file.png')
    

    在这里,定位器将是CSS id 或文件上传路径的类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多