ruby 如何在 selenium-webdriver 中获取窗口标题、ID 和名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9692460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get window titles, ids, and names in selenium-webdriver?
提问by RaymundS
Im trying to implement the following methods from selenium-webdriver(ruby)
我试图从selenium-webdriver(ruby)实现以下方法
- get_all_window_ids
- get_all_window_titles
- get_all_window_names
- get_all_window_ids
- get_all_window_titles
- get_all_window_names
- I ran Selenium IDE and exported my script to Ruby Test::Unit. Saved it as .rb
- Opened my script for editing using Aptana Studio 3
- Initial code snippet as follows:
- 我运行 Selenium IDE 并将我的脚本导出到 Ruby Test::Unit。将其另存为 .rb
- 使用 Aptana Studio 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
The error I keep getting is
我不断收到的错误是
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'
I've studied the documentation for the ruby bindings for selenium-webdriver
我已经研究了 selenium-webdriver 的 ruby 绑定文档
Ultimately my goal here is to run my automation script:
最终我的目标是运行我的自动化脚本:
- Click on a link that opens a new window with target=_blank and no windowID available (does not implement JS)
- Identify the names of all opened windows in the browser
- switch to a new pop-up window using the switchToWindow(name) method
- continue running my script on that pop-up window
- 单击一个链接,打开一个带有 target=_blank 且没有 windowID 可用的新窗口(不实现 JS)
- 识别浏览器中所有打开窗口的名称
- 使用 switchToWindow(name) 方法切换到新的弹出窗口
- 继续在该弹出窗口上运行我的脚本
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]
- Browser: Firefox 9.0.1 (Mac)
- Chrome: Chrome 17.0.963.79 (Mac)
- Selenium-Server: Ruby gem 2.20.0
- 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)
- 硒服务器:Ruby gem 2.20.0
回答by Dave Haeffner
Justin, you have a good approach. But there is a gotcha in assuming that the window handles will return in the correct order. This isn't always the case across all browsers. I outline a slightly different approach in my free weekly Selenium tip newsletter (elemental-selenium.com).
贾斯汀,你的方法很好。但是假设窗口句柄会以正确的顺序返回有一个问题。并非所有浏览器都如此。我在我的免费每周 Selenium 提示通讯 (elemental-selenium.com) 中概述了一种略有不同的方法。
It goes like this:
它是这样的:
@driver.get 'http://the-internet.herokuapp.com/windows'
main_window = @driver.window_handle
@driver.find_element(css: '.example a').click
windows = @driver.window_handles
windows.each do |window|
if main_window != window
@new_window = window
end
end
@driver.switch_to.window(main_window)
@driver.title.should_not =~ /New Window/
@driver.switch_to.window(@new_window)
@driver.title.should =~ /New Window/
You can see the full tip here.
您可以在此处查看完整提示。
回答by Justin Ko
The problem is that the get_all_window_idsis 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.
问题是它get_all_window_ids用于 Selenium::Client 而不是 Selenium::Webdriver。我相信 Selenium::Client 是旧版本的 Selenium,API 与 Selenium::Webdriver 不同(请参阅此处)。由于您使用的是 Selenium::Webdriver,这就解释了为什么会出现“未定义方法”错误。
For Selenium::Webdriver, the only way I know how to switch between windows is using:
对于 Selenium::Webdriver,我知道如何在窗口之间切换的唯一方法是使用:
@driver.switch_to.window("<window_handle>")
You can get all the known window_handles by:
您可以通过以下方式获取所有已知的 window_handles:
@driver.window_handles
#=> Returns all window handles as an array of strings
If you want to switch to the popup you just opened you can do the following. Note that this assumes .window_handlesare in the order that the windows were opened, which I believe is true:
如果要切换到刚刚打开的弹出窗口,可以执行以下操作。请注意,这假设.window_handles是按照打开窗户的顺序,我认为这是正确的:
@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
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 @driverwill automatically point back to the original window.
请注意,如果在处理弹出窗口后,您想返回原始窗口,那么我建议您执行以下操作。通过将块传递给switch_to.window,块将在弹出窗口中执行,当块结束时@driver将自动指向原始窗口。
#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

