【问题标题】:VBA - Trying to Input text and click Search button, All within a Table, within a Form, No ID Element, only Class ElementVBA - 尝试输入文本并单击“搜索”按钮,表格内,表单内,无 ID 元素,只有类元素
【发布时间】:2020-02-03 17:02:59
【问题描述】:

尝试在输入框中输入一个值,然后按“搜索”按钮从表格中获取结果。

我似乎无法弄清楚如何将值输入到框中。输入框和按钮似乎在表格内的表单内。没有 ID 元素。

**> Input Box has these properties**
<td Class = "searchRow">
<input type = "text" name = "name" value onblur = "Numberfield();" style = "width: 200px;" class = "required">
This is all inside Form ID "SearchForm" > div ID "Search" > Tr > Td CLass "searchRow"

获取正确的 Internet Explorer 选项卡后,我需要 getElementByClassName 和 inp9ut 值

 IE.Document.getElementsByTagName("tr").getElementsByTagName("input").Value = "Search"
 IE.Document.getElementsByClassName("searchRow").getElementsByTagName("input").Value = "Search"

我似乎无法让任何组合起作用。

另一个元素是按钮。

Button has these properties
<td colspan = "6" style = "align:right;" class = "searchonly">
<input type = "button" class = "actionbutton" value = "search" onclick = "submitform()">

如何单击表格行中的此按钮。

 IE.Document.getElementsByClassName("searchonly").getElementsByTagName("actionbutton").Click

这有意义吗?

感谢您的帮助。

【问题讨论】:

    标签: excel vba internet-explorer


    【解决方案1】:

    最好添加 ID 属性,然后使用 getElementsByID。

    如果这是不可能的,那么您可以使用 XPATH 并使用以下函数获取元素。

    function getElementByXpath(path) {
      return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }
    console.log( getElementByXpath("VALUE OF XPATH") );
    

    您可以通过在任何浏览器中使用扩展轻松获取 XPATH。就像 google chrome 中提供的 XPath Finder。

    【讨论】:

      【解决方案2】:

      我将答案写为 cmets 和代码:

      'What you want first
      '**> Input Box has these properties**
      '<td Class = "searchRow">
      ' <input type = "text" name = "name" value onblur = "Numberfield();" style = "width: 200px;" class = "required">
      'This is all inside Form ID "SearchForm" > div ID "Search" > Tr > Td CLass "searchRow"
      
      'What you try
      'IE.Document.getElementsByTagName("tr").getElementsByTagName("input").Value = "Search"
      'IE.Document.getElementsByClassName("searchRow").getElementsByTagName("input").Value = "Search"
      
      'What should work
      'Attention: the get methods work case sensitive. It's a difference between "Search" and "search"
      'I write that because I wonder if the first letter of the ID "Search" is upper case in the original document
      '
      'You can seperate the div tag include all inner tags as own DOM object
      Dim nodeDivSearch As Object
      Set nodeDivSearch = IE.Document.getElementByID("Search")
      '
      'Now you can work on the new DOM object only
      'Note that the getElements methods always create a NodeCollection
      'All elements of the collection have an index. The first element always has
      'index 0, so you can access the first input tag in the new DOM object as follows
      '(getElementByID does not create a NodeCollection, because an ID should always
      'be unique. So there is only one element for this criterion anyway)
      nodeDivSearch.getElementsByTagName("input")(0).Value = "Search"
      '
      'I think your code will work with setting indexes
      IE.Document.getElementsByClassName("searchRow")(0).getElementsByTagName("input")(0).Value = "Search"
      
      
      'What you want second
      'Button has these properties
      '<td colspan = "6" style = "align:right;" class = "searchonly">
      '  <input type = "button" class = "actionbutton" value = "search" onclick = "submitform()">
      
      'What you try:
      'IE.Document.getElementsByClassName("searchonly").getElementsByTagName("actionbutton").Click
      
      'I think you know what to do here
      IE.Document.getElementsByClassName("searchonly")(0).getElementsByTagName("actionbutton")(0).Click
      

      【讨论】:

        【解决方案3】:

        getElementsByClassName 返回具有所有给定类名的所有子元素的类数组对象。因此,您需要使用返回数组中的索引来选择您需要的元素。 getElementsByTagName 也是如此。

        所以如果html代码如下:

        <form id="SearchForm">
            <div id="Search">
                <table>
                    <tr>
                        <td Class="searchRow">
                            <input type="text" name="name" value onblur="Numberfield();" style="width: 200px;" class="required" id="a1">
                        </td>
                        <td colspan="6" style="align:right;" class="searchonly">
                            <input type="button" class="actionbutton" value="search" onclick="submitform()">
                        </td>
                    </tr>
                </table>
            </div>
        </form>
        

        那么设置值和点击按钮的vba代码应该如下:

        IE.Document.getElementsByClassName("searchRow")(0).getElementsByTagName("input")(0).Value = "Search"
        
        IE.Document.getElementsByClassName("searchonly")(0).getElementsByClassName("actionbutton")(0).Click
        

        以上代码仅为举例,请根据所访问网站的实际情况更改索引。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-18
          • 1970-01-01
          相关资源
          最近更新 更多