网页不接受从vba发送的文本

时间:2018-09-22 13:39:42

标签: html excel vba excel-vba web-scraping

Sub login()

Dim objie As InternetExplorer

    Set objie = New InternetExplorer

    objie.Visible = True

    objie.navigate "https://kite.zerodha.com/"

    Do While objie.Busy = True Or objie.readyState <> 4: DoEvents: Loop
    objie.document.getElementsByTagName("input")(0).Value = "abcdefgh"

    objie.document.getElementsByTagName("input")(1).Value = "abcdefgh"
    objie.document.getElementsByTagName("button")(0).Click


End Sub

我要自动化的网站不接受从vba发送的文本。所以我尝试了sendkeys方法,它起作用了。但是我想在后台运行Internet Explorer。有什么办法做到这一点。

1 个答案:

答案 0 :(得分:1)

在这种情况下,我将使用selenium basic。它要可靠得多。安装后,在VBE>工具>引用中,添加对硒类型库的引用。

Option Explicit
Public Sub EnterInfo()
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const URL = "https://kite.zerodha.com/"
    With d
        .AddArgument "--headless"
        .Start "Chrome"
        .get URL
        .FindElementByCss("[type=text]").SendKeys "abcdef"
        .FindElementByCss("[type=password]").SendKeys "123456"
        .FindElementByCss("[type=submit]").Click
        Stop '<== Delete me later
        '.Quit '<==Uncomment me later
    End With
End Sub

使用IE,您可以添加.Focus并按以下方式使用.innerText

Option Explicit
Sub login()
    Dim objie As InternetExplorer
    Set objie = New InternetExplorer
    objie.Visible = False
    objie.navigate "https://kite.zerodha.com/"

    Do While objie.Busy = True Or objie.readyState <> 4: DoEvents: Loop

    With objie.document
        .querySelector("[type=text]").Focus
        .querySelector("[type=text]").innerText = "abcdef"
        .querySelector("[type=password]").Focus
        .querySelector("[type=password]").innerText = "123456"
        .querySelector("[type=submit]").Click
    End With
End Sub