如何在 selenium-webdriver 中获取窗口标题、ID 和名称? [英] How do you get window titles, ids, and names in selenium-webdriver?

查看:166
本文介绍了如何在 selenium-webdriver 中获取窗口标题、ID 和名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 selenium-webdriver (ruby)

Im trying to implement the following methods from selenium-webdriver (ruby)

  • get_all_window_ids
  • get_all_window_titles
  • get_all_window_names
  1. 我运行了 Selenium IDE 并将我的脚本导出到 Ruby Test::Unit.将其另存为 .rb
  2. 使用 Aptana Studio 3 打开我的脚本进行编辑
  3. 初始代码片段如下:

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

class SwitchToPopup3 < Test::Unit::TestCase

  def setup
    @driver = Selenium::WebDriver.for :firefox
    @base_url = (URL of my test website)
    @driver.manage.timeouts.implicit_wait = 30
    @verification_errors = []
  end

  def teardown
    @driver.quit
    assert_equal [], @verification_errors
  end


def test_switch_to_popup3
  .
  .
  puts @driver.get_all_window_ids()
  puts @driver.get_all_window_titles()
  puts @driver.get_all_window_names()
  .
  .
end

我不断收到的错误是

NoMethodError: undefined method `get_all_window_ids' for #    <Selenium::WebDriver::Driver:0x101e4b040 browser=:chrome>
/Users/rsucgang/Documents/Aptana Studio 3 Workspace/Testing/SwitchToPopup2.rb:37:in `test_switch_to_popup3'

我研究了 selenium-webdriver 的 ruby​​ 绑定文档

I've studied the documentation for the ruby bindings for selenium-webdriver

http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/Client/GeneratedDriver.html#get_all_window_titles-instance_method

最终我的目标是运行我的自动化脚本:

Ultimately my goal here is to run my automation script:

  1. 点击一个链接会打开一个新窗口,target=_blank 并且没有可用的 windowID(没有实现 JS)
  2. 识别浏览器中所有打开窗口的名称
  3. 使用 switchToWindow(name) 方法切换到一个新的弹出窗口
  4. 继续在弹出窗口上运行我的脚本

我在互联网上搜索并研究了这个,但我没有得到任何信息.

I've googled and researched this on the internet and I have not gotten any much information.

谢谢,如果您需要更多信息,请告诉我.

Thanks and please let me know if you need more information.

  • OSL Mac OSX 10.7.3
  • Ruby:ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
  • 浏览器:Firefox 9.0.1 (Mac)
  • Chrome:Chrome 17.0.963.79 (Mac)
  • Selenium-Server:Ruby gem 2.20.0

推荐答案

问题在于 get_all_window_ids 是针对 Selenium::Client 而不是 Selenium::Webdriver.我相信 Selenium::Client 是旧版本的 Selenium,API 与 Selenium::Webdriver 不同(请参阅 此处).由于您使用的是 Selenium::Webdriver,这就解释了为什么会出现未定义方法"错误.

The problem is that the get_all_window_ids is for Selenium::Client rather than Selenium::Webdriver. I believe that Selenium::Client is the old version Selenium and the API is not the same as Selenium::Webdriver (see here). Since you are using Selenium::Webdriver, this explains why you get an 'undefined method' error.

对于 Selenium::Webdriver,我知道如何在窗口之间切换的唯一方法是使用:

For Selenium::Webdriver, the only way I know how to switch between windows is using:

@driver.switch_to.window("<window_handle>")

您可以通过以下方式获取所有已知的 window_handles:

You can get all the known window_handles by:

@driver.window_handles
#=> Returns all window handles as an array of strings

如果您想切换到刚刚打开的弹出窗口,您可以执行以下操作.请注意,这假设 .window_handles 是按照窗口打开的顺序排列的,我认为这是正确的:

If you want to switch to the popup you just opened you can do the following. Note that this assumes .window_handles are in the order that the windows were opened, which I believe is true:

@driver.switch_to.window @driver.window_handles.last

总而言之,假设您只关心访问弹出窗口(而不关心按名称访问),您可以这样做:

To summarize, assuming you only care about accessing the popup (and not about accessing by name) you can do:

#Click control that opens popup
@driver.find_element(:id, 'button that opens popup').click
#Switch to popup
@driver.switch_to.window @driver.window_handles.last
#Do actions in new popup
@driver.find_element(:id, 'id of element in popup').click

请注意,如果在处理弹出窗口后,您想返回原始窗口,那么我建议您执行以下操作.通过将块传递给 switch_to.window,块将在弹出窗口中执行,当块结束时 @driver 将自动指向原始窗口.>

Note that if after working with the popup, you will want to return to the original window, then I suggest you do the following. By passing a block to the switch_to.window, the block will be executed in the popup and when the block ends @driver will automatically point back to the original window.

#Click control that opens popup
@driver.find_element(:id, 'button that opens popup').click
#Switch to popup
@driver.switch_to.window( @driver.window_handles.last ){
  #Do actions in new popup
  @driver.find_element(:id, 'id of element in popup').click
}
#Continue with original window
@driver.find_element(:id, 'button in original window').click

这篇关于如何在 selenium-webdriver 中获取窗口标题、ID 和名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆