2017-08-04 13 views
0

stackoverflowのすべてのスレッドを検索しましたが、私の問題に関して適切な解決策が見つかりませんでした。 7/10のケースでは、スクリプトが特定の要素を1つ見つけられないためにスクリプトが低下しています。したがって、私はスクリプトに "この要素が存在するまで待ってからそれをクリックしてください"と言うためにoppurtunityを探しています。要素が存在するまで待つ

私のコードは次のようになります。

require "json" 
require "selenium-webdriver" 
require "test/unit" 

class LogIn < Test::Unit::TestCase 

def setup 
@driver = Selenium::WebDriver.for :chrome 
@base_url = "url" 
@accept_next_alert = true 
@driver.manage.timeouts.implicit_wait = 30 
@verification_errors = [] 
end 
def teardown 
@driver.quit 
assert_equal [], @verification_errors 
end 
def test_sms_newsletter 
#login 
@driver.get "https://secure.shore.com/merchant/sign_in" 
@driver.find_element(:id, "merchant_account_email").clear 
@driver.find_element(:id, "merchant_account_email").send_keys "key" 
@driver.find_element(:id, "merchant_account_password").clear 
@driver.find_element(:id, "merchant_account_password").send_keys 
"password" 
@driver.find_element(:name, "button").click 
@driver.get "https://secure.shore.com" 
verify { assert_equal "Shore - Manage your customers", @driver.title } 
sleep 5 
@driver.find_element(:css, "#asMain > header > a.as-Button.is- 
active.as-Button--inverted").click 

私が取っている要素が最後のものである:

@driver.find_element(:css, "#asMain > header > a.as-Button.is-active.as-Button--inverted").click 

誰もがアイデアを持っていますか?

+0

表示されるエラーは何ですか? @ driver.find_element(:css、 "#asMain> header> a.as-Button.is-active.as-Button - inverted")を使って何をするかを正確に更新してください。 https:// secure.shore.com'。ありがとう – DebanjanB

+2

適切なインデントは巨大なデバッグヘルプです –

+0

こんにちは@DebanjanB。フロントエンドが良い方法で書かれていないので、この要素を時間内に見つけるのに苦労しているスクリプトです。したがって、私はスクリプトが最初に要素を見つけてから続行するように強制します。 –

答えて

0

お待ちください。 FluentWaitを使用してみてください。

問題が解決しない場合は、JavascriptExecutorを使用してください。それはJSを通して直接動作します。ロケータが正常であれば動作するはずです。私はJavascriptExecutor

elementfield = browser.find_element(:css, "#asMain > header > a.as-Button.is-active.as-Button--inverted") 
browser.execute_script('arguments[0].click();', elementfield) 

を使って任意の要素をクリックする例を与えている、それはあなたを助けることを願っています:)ここで

0

は、要素が存在するのを待つためのスニペットです。あなたはそれを使用する前に、すべての要素の前にこのようなブロックを置くことができます。ブロックがタイムアウトすると、テストは失敗します。

require 'rubygems' 
require 'selenium-webdriver' 

browser = Selenium::WebDriver.for :firefox 
browser.get "http://localhost/page3" 

wait = Selenium::WebDriver::Wait.new(:timeout => 15) 

# Add text to a text box 
input = wait.until { 
    element = browser.find_element(:css, "#asMain > header > a.as-Button.is-active.as-Button--inverted") 
    element if element.displayed? 
} 
input.send_keys("Information") 
+0

こんにちはケビン、私はあなたのsugesstionを試みたが、私はうまくいかなかった。 'Error:test_sms_newsletter(LogIn):NameError:未定義のローカル変数またはメソッドがを待っています –

関連する問題