2

Selenium と Ruby を使用して、仕事の連絡先を自動的に更新しようとしています。Web ページ上の名前と一致する連絡先名の CSV があります。Web ページには、一度に 50 件の連絡先しか表示されず、[次へ] ボタンで次に進みます。Ruby スクリプトが名前 (つまり、51 番目の連絡先) に到達すると、スクリプトは最初のページではなく次のページにある"Barbara"ため、要素を見つけることができません。"Barbara"

WebDriver がページ要素を見つけられない場合、次のエラーが発生します。

失敗: 1) RackspaceAutomation test_rackspace_automation 失敗/エラー: @driver.find_element(:link, row[0]).click Selenium::WebDriver::Error:NoSuchElementError: 要素が見つかりません: {"method":"link text", "selector:":"バーバラ"}

プログラムを終了します。代わりに、指定された名前が見つからない場合は、@driver.find_element(:id, "Next").click行を実行して名前を再度検索します。

エラーを処理するためにいくつかの変更を加えました。これまでのコード:

CSV.foreach('C:\Users\James\SeleniumTests\WebbContactsFullCVS.cvs') do |行|

  begin
    @driver.find_element(:link, row[0]).click
    @driver.find_element(:link, "Contact Information").click
    # ERROR: Caught exception [ReferenceError: selectLocator is not defined]
    a=@driver.find_element(:id,'PhoneNumberType')
    options=a.find_elements(:tag_name=>"option")
    options.each do |g|
      if g.text == "Mobile"
        g.click
        break
      end
    end
    @driver.find_element(:id, "MobilePhone").send_keys row[1]
    # ERROR: Caught exception [ReferenceError: selectLocator is not defined]
    options.each do |g|
      if g.text == "Fax"
        g.click
        break
      end
    end
    @driver.find_element(:id, "Fax").send_keys row[2]
    @driver.find_element(:css, "button.primary").click
  rescue NoSuchElementError
    @driver.find_element(:id, "Next").click
    retry
  end
end

エラーの取得:

失敗: 1) RackspaceAutomation test_rackspace_automation 失敗/エラー: レスキュー NoSuchElementError NameError: 未初期化定数 NoSuchElementError # ./RackspaceAutomation.rb:57:in 'rescue in block (3 レベル) in ' # ./RackspaceAutomation.rb:35:in 'block ( 3 レベル) in ' # ./RackspaceAutomation.rb:35:in 'ブロック (2 レベル) in '

、Rubyが初めてなので、初期化する方法/場所がわかりません。どんな助けでも素晴らしいでしょう!

4

1 に答える 1

3
class NoSuchElementError < Exception
end

names = %w[ a b c ]
on_page = %w[ a b ]

names.each do |name|
  begin
    raise NoSuchElementError if not on_page.include? name
  rescue NoSuchElementError
    puts "rescuing: #{name}"
    on_page = %w[c d]
    retry
  end
end

--output:--
rescuing: c

したがって、次のようなことができます。

names.each do |name|
  begin
    #error throwing code here
  rescue NoSuchElementError
    @driver.find_element(:id, "Next").click
    retry
  end
end
于 2013-09-05T05:00:42.097 に答える