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

如何使用Chrome.ahk库修改iframe内的输入字段?

用Chrome.ahk填充iframe内字段失败求解决

我之前通过IE对象能成功填充iframe内的字段,对应代码如下:

workorder := "test"
frame := pwb.document.parentWindow.frames("uxtabiframe-1068-frame")
frame.document.GetElementsByName("ff_workordernum").item[0].Value := workorder

但尝试用Chrome.ahk库实现相同功能时多次失败,试过以下两种方法均报错:

方法1

PageInstance.Evaluate("document.parentWindow.frames('uxtabiframe-1068-frame').document.GetElementsByName('ff_workordernum').item[0].Value = 'test'")

方法2

iframeJs =
(
var iframe = document.getElementById('uxtabiframe-1068-frame');
var test2 = iframe.contentDocument..GetElementsByName('ff_workordernum').item[0].Value = 'test';
)
PageInstance.Evaluate(iframeJs)

解决方案

你的代码存在几处语法和API使用错误,修正后即可正常运行:

  1. 方法1修正:

    • JavaScript中访问frames集合需使用方括号[],而非IE兼容的圆括号()
    • 原代码缺少闭合双引号,且JS中DOM方法和属性为小写(getElementsByName、value,而非大写的GetElementsByName、Value)
    • 修正后代码:
      PageInstance.Evaluate("document.parentWindow.frames['uxtabiframe-1068-frame'].document.getElementsByName('ff_workordernum')[0].value = 'test';")
      
  2. 方法2修正:

    • contentDocument后多了一个冗余的点(..)
    • 同样需修正DOM方法和属性的大小写,且无需额外赋值给test2变量
    • 修正后代码:
      iframeJs =
      (
      var iframe = document.getElementById('uxtabiframe-1068-frame');
      iframe.contentDocument.getElementsByName('ff_workordernum')[0].value = 'test';
      )
      PageInstance.Evaluate(iframeJs)
      

额外注意:如果iframe与主页面跨域,Chrome会因同源策略限制访问。测试环境下可在启动Chrome时添加--disable-web-security参数绕过该限制,但生产环境不建议使用。

内容的提问来源于stack exchange,提问作者Luis

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 06:30:47