You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Watir结合Selenium在Firefox与Chrome中处理iframe时的wait_until及.html方法异常问题

Watir结合Selenium在Firefox与Chrome中处理iframe时的wait_until及.html方法异常问题

环境配置

  • Watir: 7.0.0
  • Selenium-webdriver: 4.0.0
  • Ruby: 3.0.6
  • OS: MacOs Version 14.4 (23E214)
  • 浏览器及驱动:最新版Firefox + geckodriver、最新版Chrome

Firefox下iframe使用wait_until后的交互异常

最近在写测试代码时发现,给iframe调用wait_until方法等待其出现后,只能对这个iframe执行一次交互操作,第二次操作就会直接失败。必须重新定位一次iframe才能继续正常交互。

测试代码

require 'watir'
Selenium::WebDriver.logger.level = :info

class TestFFIssueWatir
  def initialize
    @browser = Watir::Browser.new :firefox
  end

  def goto_website
    @browser.goto('https://demo.automationtesting.in/Frames.html')
  end

  def close_browser
    @browser.close
  end

  # 演示Firefox中使用wait_until后iframe的交互问题
  # 调用wait_until后,只能对该iframe执行一次操作,第二次send_keys会失败
  def check_error_using_wait_until
    # 定位iframe
    top_dialog = @browser.body
    testIframe = top_dialog.iframe(id: 'singleframe')

    # 调用wait_until后,后续只能执行一次交互操作
    testIframe.wait_until(timeout: 10, &:present?)

    # 第一次输入正常,第二次输入会失败
    testIframe.input.send_keys('Hello, Universe!')
    testIframe.input.send_keys('Hello, Again!')

    sleep(10)
  end
end

test_watir = TestFFIssueWatir.new
test_watir.goto_website
test_watir.check_error_using_wait_until
test_watir.close_browser

错误日志片段

RemoteError.sys.mjs:8:8\nWebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:191:5\nNoSuchFrameError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:570:5\nswitchToFrame@chrome://remote/content/marionette/actors/MarionetteCommandsChild.sys.mjs:567:15\nreceiveMessage@chrome://remote/content/marionette/actors/MarionetteCommandsChild.sys.mjs:154:31\n"}}
2024-02-13 12:13:00 INFO Selenium -> GET session/fc12f525-e753-4b0c-ba1b-11e49a461139/element/c42da6c3-bbf3-4619-9818-d7c47ba559d3/css/staleness_check
2024-02-13 12:13:00 INFO Selenium <- {"value":""}
2024-02-13 12:13:00 INFO Selenium -> POST session/fc12f525-e753-4b0c-ba1b-11e49a461139/frame
2024-02-13 12:13:00 INFO Selenium    >>> http://127.0.0.1:4445/session/fc12f525-e753-4b0c-ba1b-11e49a461139/frame | {"id":{"element-6066-11e4-a52e-4f735466cecf":"c42da6c3-bbf3-4619-9818-d7c47ba559d3"}}
2024-02-13 12:13:00 INFO Selenium <- {"value":{"error":"no such frame","message":"Unable to locate frame for element: [object HTMLIFrameElement]","stacktrace":"RemoteError@chrome://remote/content/shared/

Chrome下iframe多次调用.html方法的失效问题

在Chrome浏览器中,对同一个iframe对象连续两次调用.html方法时,第二次会直接抛出元素过期或找不到的异常。

测试代码

require 'watir'
Selenium::WebDriver.logger.level = :debug
@browser = Watir::Browser.new :chrome
@browser.goto('https://demo.automationtesting.in/Frames.html')
testIframe = @browser.body.iframe(name: 'SingleFrame')
puts(testIframe.html)  # 第一次输出正常
puts(testIframe.html)  # 第二次执行抛出异常

错误信息

stale element reference: stale element not found (Selenium::WebDriver::Error::StaleElementReferenceError)
# 或者出现另一种错误提示
no such element: element not found (Selenium::WebDriver::Error::NoSuchElementError)

这些操作在旧版本的浏览器中都能正常运行,但在浏览器和驱动更新到最新版本后就出现了上述问题。想问问有没有其他开发者也遇到过类似的情况?或者有没有可行的解决方案可以分享?

备注:内容来源于stack exchange,提问作者Santosh Adhikari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 12:03:12