您说您可以找到该搜索框的名称。因此,您应该尝试使用 VBA 代码中的名称来访问它。
以下是示例 VBA 代码:
Sub demo()
Dim IE As Object
Dim ws As Worksheet
Set IE = CreateObject("InternetExplorer.Application")
Set ws = ThisWorkbook.Worksheets("Sheet1")
IE.Visible = True
IE.Navigate ("C:\Users\Administrator\Desktop\search_demo1.html")
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.document.getElementsByName("search2")(0).Value = ws.Range("A1").Value2
IE.document.getElementById("submit").Click
End Sub
示例 HTML 代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
font-family: Arial;
}
* {
box-sizing: border-box;
}
form.example input[type=text] {
padding: 10px;
font-size: 17px;
border: 1px solid grey;
float: left;
width: 80%;
background: #f1f1f1;
}
form.example button {
float: left;
width: 20%;
padding: 10px;
background: #2196F3;
color: white;
font-size: 17px;
border: 1px solid grey;
border-left: none;
cursor: pointer;
}
form.example button:hover {
background: #0b7dda;
}
form.example::after {
content: "";
clear: both;
display: table;
}
#lbl
{
color: red;
}
</style>
<script>
function search()
{
var div = document.getElementById('lbl');
var div1 = document.getElementsByName('search2')[0].value;
div.innerHTML += 'Searching the results for '+ div1;
}
</script>
</head>
<body>
<h2>Sample Test</h2>
<input type="text" placeholder="Search.." name="search2" value="">
<button type="submit" onclick="search()"><i class="fa fa-search"></i></button><br>
<div id=lbl></div>
</body>
</html>
输出:
