【问题标题】:Excel VBA browser automation: website search box has no element idExcel VBA 浏览器自动化:网站搜索框没有元素 ID
【发布时间】:2017-05-23 21:47:15
【问题描述】:

我正在尝试使用 Excel VBA 进行浏览器自动化,但网站的 (http://www.soccerstats.com/) 搜索框没有元素 ID,因此通常的技术不起作用。 这是我计划的代码:

    'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link

'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer

'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True

'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "http://www.soccerstats.com/"

'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("dsearch").Value = "arsenal"

搜索框html代码为:

&lt;input name="searchstring" style="border: currentColor; border-image: none; width: 90px; height: 20px; padding-left: 5px; background-color: rgb(238, 238, 238); text-color: gray;" onfocus="if (this.value == 'Search team...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search team...';}" type="text" maxlength="20" value="Search team..."&gt;

谁能告诉我如何为这个搜索框输入一个值,然后点击搜索按钮?

【问题讨论】:

标签: excel vba browser automation


【解决方案1】:

您可以遍历输入元素并找到名为searchstring 的元素,如下所示:

Dim ele As Object

For each ele in objIE.document.getElementsByTagName("input")
    If ele.Name = "searchstring" Then
        ele.Value = "Example Text"
    End if
Next ele

【讨论】:

  • input 元素有一个name
  • @Jordan 我也想点击按钮,我试过了,但它似乎不起作用: For Each ele In objIE.document.getElementsByTagName("input") If ele.Name = "submit" Then ele.Click End If Next ele
  • 提交按钮的 HTML 是什么?
  • '' 好像没有名字?我搞混了,提交是一个类。谢谢
  • 设法点击按钮!代码听到任何人需要它。再次感谢您的帮助'For Each ele In objIE.document.getElementsByTagName("input") If ele.className = "submit" Then ele.Click End If Next ele'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 2011-06-01
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 2012-05-14
  • 2012-07-31
相关资源
最近更新 更多