例如:
# encoding: utf-8 require "capybara/dsl" Capybara.run_server = false Capybara.current_driver = :selenium Capybara.app_host = 'https://hb.posted.co.rs/posted' class Account include Capybara::DSL def check_balance visit('/') page.driver.browser.switch_to.frame 'main' fill_in 'korisnik',:with => 'foo' fill_in 'lozinka',:with => 'bar' click_button 'Potvrda unosa' page.driver.browser.switch_to.frame 'header' click_on 'Stanje' end end account = Account.new account.check_balance
错误是:
[remote server]
file:///tmp/webdriver-profile20120810-9163-xy6dtm/extensions/fxdriver@googlecode.com/components/driver_component.js:6638:in
`unknown’: Unable to locate frame: main
(Selenium::WebDriver::Error::NoSuchFrameError)
问题是什么?也许我在这里做错事?
如果我改变切换帧的顺序,那么先尝试切换到’标题’,然后切换到’主’帧,然后相同的错误提高,除了它说这次没有’主’帧:
# encoding: utf-8 require "capybara/dsl" Capybara.run_server = false Capybara.current_driver = :selenium Capybara.app_host = 'https://hb.posted.co.rs/posted' class Account include Capybara::DSL def check_balance visit('/') page.driver.browser.switch_to.frame 'header' click_on 'Stanje' page.driver.browser.switch_to.frame 'main' fill_in 'korisnik',:with => 'bar' click_button 'Potvrda unosa' end end account = Account.new account.check_balance
错误:
[remote server]
file:///tmp/webdriver-profile20120810-9247-w3o5hj/extensions/fxdriver@googlecode.com/components/driver_component.js:6638:in
`unknown’: Unable to locate frame: main
(Selenium::WebDriver::Error::NoSuchFrameError)
解决方法
问题是当您执行page.driver.browser.switch_to.frame时,它将页面的上下文切换到框架.所有针对该页面的操作现在实际上都是针对该框架的.所以当你第二次切换帧时,你实际上是在’main’框架内找到’header’框架(而不是我假设你想要的,主页面的’header’框架).
解决方案 – Capybara in_frame(推荐):
在框架内工作时,您应该使用Capybara的in_frame方法.你想做:
def check_balance visit('/') within_frame('main'){ fill_in 'korisnik',:with => 'foo' fill_in 'lozinka',:with => 'bar' click_button 'Potvrda unosa' } within_frame('header'){ click_on 'Stanje' } end
解决方案 – Selenium switch_to:
如果您想自己进行框架管理(即不使用Capybara的内置方法),则可以将页面的上下文切换回浏览器,然后再转到第二个框架.这将如下所示.虽然我建议使用内置的Capybara方法.
def check_balance visit('/') page.driver.browser.switch_to.frame 'header' click_on 'Stanje' #Switch page context back to the main browser page.driver.browser.switch_to.default_content page.driver.browser.switch_to.frame 'main' fill_in 'korisnik',:with => 'bar' click_button 'Potvrda unosa' end