gpt4 book ai didi

ruby - 使用 Selenium Webdriver 切换窗口时出错

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:22 25 4
gpt4 key购买 nike

我编写了一个 Ruby 脚本来使用 IE 自动执行一些用户操作。我正在为 IE 使用 Selenium Web 驱动程序。下面是我的代码。

require 'selenium-webdriver'

browser = Selenium::WebDriver.for :ie

first_window = browser.window_handle
browser.switch_to.frame(browser.find_element(:id=> 'outerFrame'))
browser.switch_to.frame(browser.find_element(:id=> 'innerFrame'))

table_rows = browser.find_element(:id=> 'AllItems').find_element(:tag_name=> 'table').find_elements(:tag_name=> 'tr')
count_cell = table_rows.at(table_rows.length-1).find_elements(:tag_name=> 'td').at(1).find_element(:tag_name=> 'a')
count_cell.click

sleep(5)

all_windows = browser.window_handles
new_window = browser.window_handles.last

browser.switch_to.window(new_window)

btn = browser.find_element(:id=> 'btn_export')
btn.click

有一次,单击一个按钮后,会打开一个新页面。现在,当我尝试切换到新窗口时,出现以下错误。

C:/Ruby21/lib/ruby/2.1.0/net/http.rb:879:in 'initialize': No connection could be made because the target machine actively refused it. - connect(2) for "127.0.0.1" port 5555 (Errno::ECONNREFUSED) from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:879:in 'open' from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:879:in 'block in connect' from C:/Ruby21/lib/ruby/2.1.0/timeout.rb:75:in 'timeout' from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:878:in 'connect' from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:863:in 'do_start' from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:852:in 'start' from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1375:in 'request' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/http/default.rb:107:in 'response_for' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/http/default.rb:58:in 'request' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/http/common.rb:59:in 'call' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/bridge.rb:664:in 'raw_execute' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/bridge.rb:642:in 'execute' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/bridge.rb:216:in 'switchToWindow' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/common/target_locator.rb:81:in 'window' from script.rb:18:in ''

我尝试通过修改防火墙并为端口 5555 添加规则来解决错误。即便如此,问题仍然存在。我发现了一些已经存在的问题,但其中大部分与 Java 或 .Net 有关。我该如何解决这个错误?谁能指出我正确的方向?

最佳答案

我不是 ruby 专家,但我见过类似的问题。我有一个 Python-ic 解决方案。您需要做的就是对代码进行 Ruby-fy。 https://gist.github.com/ab9-er/08c3ce7d2d5cdfa94bc7

def change_window(browser):
"""
Simple window switcher without the need of playing with ids.
@param browser: Current browser instance
"""
curr = browser.current_window_handle
all_handles = browser.window_handles
for handle in list(set([curr]) - set(all_handles)):
return browser.switch_to_window(handle)

关于ruby - 使用 Selenium Webdriver 切换窗口时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34082836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com