【问题标题】:How can I switch between two frames with Capybara如何使用 Capybara 在两个帧之间切换
【发布时间】:2012-08-11 08:19:32
【问题描述】:

我正在尝试在 2 帧内执行某些操作,但每次尝试在帧之间切换时都会出现错误。 例如:

# 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

错误是:

[远程服务器] file:///tmp/webdriver-profile20120810-9163-xy6dtm/extensions/fxdriver@googlecode.com/components/driver_component.js:6638:in `未知':无法找到框架:主要 (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 => 'foo'
    fill_in 'lozinka', :with => 'bar'
    click_button 'Potvrda unosa'
  end
end

account = Account.new
account.check_balance

错误:

[远程服务器] file:///tmp/webdriver-profile20120810-9247-w3o5hj/extensions/fxdriver@googlecode.com/components/driver_component.js:6638:in `未知':无法找到框架:主要 (Selenium::WebDriver::Error::NoSuchFrameError)

【问题讨论】:

    标签: ruby capybara


    【解决方案1】:

    问题

    问题是当您执行page.driver.browser.switch_to.frame 时,它会将页面的上下文切换到框架。现在所有针对页面的操作实际上都是针对框架的。因此,当您第二次切换框架时,您实际上是在说在“主”框架内找到“标题”框架(而不是我假设您想要的,主页内的“标题”框架)。

    解决方案 - Capybara inside_frame(推荐):

    在框架内工作时,应使用 Capybara 的 within_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 => 'foo'
        fill_in 'lozinka', :with => 'bar'
        click_button 'Potvrda unosa'
      end
    

    【讨论】:

    • 太棒了..帮了我很多忙!
    【解决方案2】:

    我找到了解决方案。 inside_frame 按预期工作:

    # 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('/')
    
        within_frame 'main' do
          fill_in 'korisnik', :with => 'foo'
          fill_in 'lozinka', :with => 'bar'
          click_button 'Potvrda unosa'
        end
    
        within_frame 'header' do
          click_on 'Stanje'
        end
      end
    end
    
    account = Account.new
    account.check_balance
    

    我在文件 https://github.com/jnicklas/capybara/blob/master/lib/capybara/selenium/driver.rb 中找到了 inside_frame 的源代码。从第 81 行开始。

    编辑: 当我写这个答案时,@JustinKo 回答了问题,所以两个答案都是正确的,但 +1 并接受了他的答案。

    【讨论】:

      猜你喜欢
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多