2017-08-04 36 views
0

我通過每個線程在stackoverflow上搜索,但找不到適合我的問題的解決方案。在7/10的情況下,我的腳本正在下降,因爲他們沒有找到一個特定的元素。因此,我正在尋找一個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

Hi @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,但我沒有工作,但。我收到一條錯誤消息: 錯誤:test_sms_newsletter(LogIn):NameError:未定義的局部變量或方法等待#