【问题标题】:working with .Document.getElementById() and variables in vba在 vba 中使用 .Document.getElementById() 和变量
【发布时间】:2020-03-06 04:42:39
【问题描述】:

我正在尝试使用vba从下拉框中选择值,下拉框的代码块如下

<input type="text" id="form_autocomplete_input-1542902425322" list="form_autocomplete_suggestions-1542902425322" placeholder="Search keyword or select filter" role="combobox" aria-expanded="false" autocomplete="off" autocorrect="off" autocapitalize="off" aria-autocomplete="list" aria-owns="form_autocomplete_suggestions-1542902425322 form_autocomplete_selection-1542902425322">

如果form_autocomplete_suggestions-1542902425322 的值是静态的,我会使用.Document.getElementById("form_autocomplete_suggestions-1542902425322").Value = "Role: Student",但这似乎是一个随机生成的数值。

我看过了,看来我不能简单地添加通配符,例如.Document.getElementById("form_autocomplete_suggestions-*").Value = "Role: Student"

而且由于它是随机生成的,而且数字如此之长,它不能循环遍历一个值数组。所以我不确定如何解决这个问题。

【问题讨论】:

  • 那么表单上会有多少个文本框?您不必遍历数字数组,您可以查看以.getElements 等开头的选项,将它们放入集合并在必要时循环它们。你能多解释一下对象的数量,或者贴一张页面的屏幕截图吗?
  • 您好,不知道您所说的表单是什么意思?问题是下拉框的值发生了变化!image 所以我不能使用.Document.getElementById("form_autocomplete_suggestions-1542902425322").Value = "Role: Student",因为 1542902425322 的值现在已更改为其他值。

标签: excel vba


【解决方案1】:

您可以使用带有^ 运算符的css 属性等于值选择器语法来表示以某个子字符串开头。您也可以改用*,表示包含。

[id^='form_autocomplete_input-']

VBA:

ie.document.querySelector("[id^='form_autocomplete_input-']")

你也可以使用:

[placeholder='Search keyword or select filter']

应该是:

ie.document.querySelector("[placeholder='Search keyword or select filter']")

正如您指出需要选择的,您可能需要:

ie.document.querySelector("[id^='form_autocomplete_input-']").Selected = True

参考:

  1. CSS attribute selectors

【讨论】:

  • 你试过了吗?
猜你喜欢
  • 1970-01-01
  • 2017-09-25
  • 2016-10-10
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多