这个看似简单,却让我绕了一大圈。 寻找一种在没有 Selenium 引用的情况下在打开的 Microsoft Edge 浏览器页面上的输入框中输入值的方法。通过按键部分完成。无法将其定位到目标输入框。
我确实遇到了这篇文章:https://stackoverflow.com/a/71994505/4075963 问题是帐户被锁定,无法编辑注册表。我无法让 CDP 识别并附加到现有/打开的 Edge 窗口。 CDP 将关闭其他打开的窗口并开始一个新实例。浏览所有页面并对它们进行编程是一件令人头疼的事情。也许如果归结为是,我会尝试从这里开始。 IES 无法定位 Edge,IE 模式已启用。
我能够使用 Adobe 成功运行代码。格式一致。 在浏览器中查看时,页面有时会出现一个复选框,使内容变得不协调。
要求: 预 - 识别并激活/附加到现有的 Microsoft Edge 窗口。 (没那么重要。)我打开它,只需选择第一个输入框即可。
问题: 基本脚本有效,我认为它在某个地方出现了滞后,导致 tab 键脱节。尝试在 Tab 键之间添加 0.01 秒的延迟。 然后意识到真正的问题是有时在几行中随机出现一个复选框。所以它抛弃了标签导航。
正在看看是否还有其他方法。 也许启用快捷功能来向下移动输入框而不是下一个? 也许将鼠标点击发送到网页坐标?使其始终单击相同的框或区域,即使它们不可见?
尝试/失败: 识别找到/附加的 Windows Edge 窗口。 - 只是不适合我。 通过id标签识别输入框。 - 没有做任何事。 通过数字和字母识别输入列索引。 - 它会出现在我激活的初始输入框中,但不会移动到下一个。
最佳尝试: 这只是模仿原始按键。按 Tab 3 次即可完成并进入该列的下一行。 问题是,有时第 1 列中会出现随机复选框。此脚本将一直有效,直到出现野生复选框为止。
输入目的地
Sub SendBrowser()
Dim startValue As Double
Dim repeatCount As Long
Dim i As Long
' Read values from cells
startValue = CDbl(Range("B1").Value)
repeatCount = CLng(Range("B3").Value)
' Wait for 5 seconds
Application.Wait Now + TimeValue("00:00:05")
' Loop through and send keys with tab
For i = 1 To repeatCount
On Error Resume Next
AppActivate "Schedule"
On Error GoTo 0
If Err.Number <> 0 Then
MsgBox "Application 'Schedule' not found!"
Exit Sub
End If
SendKeys CStr(startValue), True
WaitMillis 100 ' Wait for 100 milliseconds
SendKeys "{TAB}", True
WaitMillis 100 ' Wait for 100 milliseconds
SendKeys "{TAB}", True
WaitMillis 100 ' Wait for 100 milliseconds
SendKeys "{TAB}", True
startValue = startValue + 1
Next i
End Sub
Sub WaitMillis(milliseconds As Long)
Dim startTime As Double
startTime = Timer
Do While Timer < startTime + (milliseconds / 1000)
DoEvents
Loop
End Sub
解决方案: 能够使用这个:https://github.com/longvh211/Edge-IE-Mode-Automation-with-IES-for-VBA
栏内的输入框生成后会进行排序。所以他们就会出问题。我能够将其插入到以 Schedule_txt 开头的下一个可用输入框中,而不是数字顺序。因此,这不是 sendKey [tab],而是直接针对输入框。
要实现此功能,您需要设置 Edge 窗口的标题目标。 您还需要上面链接中的核心模块。
边缘自动化无需 Selenium 即可工作。
Sub Input()
Dim startValue As Double
Dim repeatCount As Long
Dim i As Long
startValue = CDbl(Range("B1").Value)
repeatCount = CLng(Range("B3").Value)
titleToFind = "Scheduled"
Set ieDoc = GetEdgeIeDOM(titleToFind)
If ieDoc Is Nothing Then
MsgBox "The webpage cannot be found on Edge IE Mode! Has it been loaded under Edge IE Mode?"
End
End If
For i = 0 To repeatCount - 1
Dim inputBoxes As Object
Set inputBoxes = ieDoc.querySelectorAll("[id^='Schedule_txt']")
If inputBoxes.Length > i Then
inputBoxes.Item(i).Value = startValue
startValue = startValue + 1 ' Increase startValue by 1
Else
MsgBox "No more input boxes with the specified ID pattern found."
Exit Sub
End If
Next i
End Sub