Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用vba登录网站的问题_Vba_Excel - Fatal编程技术网

使用vba登录网站的问题

使用vba登录网站的问题,vba,excel,Vba,Excel,我正在尝试自动登录网站,每次登录时,我都会收到对象“IWebBrowser2”失败的“方法“文档”错误。该网站是内联网,它很可能是不可访问的,但是,我将提供我正在查看的代码 它使用java和html,名称相同,ID名称相同。我尝试过使用确切的名称,也尝试过使用通配符,但仍然会出现上述错误。我也尝试过使用姓名和ID,但这两种方式似乎都不适合我 HTML Code <TD class=style6><SPAN style="FONT-SIZE: 9pt" id=ContentPl

我正在尝试自动登录网站,每次登录时,我都会收到对象“IWebBrowser2”失败的“方法“文档”错误。该网站是内联网,它很可能是不可访问的,但是,我将提供我正在查看的代码

它使用java和html,名称相同,ID名称相同。我尝试过使用确切的名称,也尝试过使用通配符,但仍然会出现上述错误。我也尝试过使用姓名和ID,但这两种方式似乎都不适合我

HTML Code

<TD class=style6><SPAN style="FONT-SIZE: 9pt" id=ContentPlaceHolder1_Label1 class=style1>VZID:</SPAN> 
<BR><INPUT style="WIDTH: 125px" id=ContentPlaceHolder1_userid class=text name=ctl00$ContentPlaceHolder1$userid> 
<BR><SPAN style="FONT-SIZE: 9pt" id=ContentPlaceHolder1_Label2 class=style1>Password:</SPAN> 
<BR><INPUT style="WIDTH: 125px" id=ContentPlaceHolder1_password class=text name=ctl00$ContentPlaceHolder1$password value="" type=password> 
<BR><BR><INPUT id=ContentPlaceHolder1_submitthis class=button name=ctl00$ContentPlaceHolder1$submitthis value=Login type=submit> </TD>
谢谢你的帮助

**编辑**

所以我试着修改延迟,但我没有得到一个接口未知错误lol

Sub sbIE_OpenVMOSite()

    uname = "username"
    pword = "password"

    myWebsite = "TheWebsite"
    fdUSERNAME = "ContentPlaceHolder1_userid"
    fdPASSWORD = "ContentPlaceHolder1_password"
    fdSUBMIT = "ContentPlaceHolder1_submitthis"

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate myWebsite
    IE.Visible = True
        'Application.Wait DateAdd("s", 5, Now)
    'Do While IE.Busy
    '    Application.Wait DateAdd("s", 5, Now)
    'Loop
    'Wend

    Do
        If IE.readyState = 4 Then
            IE.Visible = False
            Exit Do
        Else
            DoEvents
        End If
    Loop


    'MsgBox ("POP")
    IE.document.getElementByID(fdUSERNAME).Value = uname  'Enters Username
    IE.document.getElementByID(fdPASSWORD).Value = pword  'Enters Password
    IE.document.all(fdSUBMIT).Click

End Sub

试试这个!我更正了fdUSERNAME(以及fdPASSWORD和fdSUBMIT),但它们可能不会造成任何问题。此外,我对代码进行了一些清理

Sub sbIE_OpenVMOSite()

    uname = "username"
    pword = "password"

    myWebsite = "http://TheWebsite"
    fdUSERNAME = "ContentPlaceHolder1_userid"
    fdPASSWORD = "ContentPlaceHolder1_password"
    fdSUBMIT = "ContentPlaceHolder1_submitthis"    

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate myWebsite
    IE.Visible = True

    While IE.Busy
        Application.Wait DateAdd("s", 2, Now)
    Wend

    IE.document.getElementById(fdUSERNAME).Value = uname      'Enters Username
    IE.document.getElementById(fdPASSWORD).Value = pword  'Enters Password    
    IE.document.all(fdSUBMIT).Click

End Sub

谢谢,我试过了,我得到了同样的错误,我也试过使用实际值,用于getElementById和getElementByName,然后是我在原始帖子中使用的通配符搜索,但它仍然返回“方法”“文档”“对象”“IWebBrowser2”“失败”,也许你的页面没有加载完毕。有时,在IE.Busy部分开始加载之前,计算机会进入
;在输入值之前,尝试添加一个
msgbox“pop!”
。此外,如果页面包含框架,则必须使用
IE.Document.frames(x).getElement…
;在VBA中,每个帧都作为自己的文档(据我所知)。没有帧,我在添加msgbox时注意到的唯一一点是,我调整了“延迟”,但它并没有像应该的那样真正暂停。。。可能是站点没有打开Yet要知道IE.Busy
时的
是否导致了问题,请用
Application.Wait DateAdd(“s”,10,Now)
替换整个循环(或任何足够高的值以确保网站完全加载)。获得一个好的“睡眠”功能有时需要微调。很抱歉,前几天我休息了。。。我回去看了看,“计时器”和你提到的一样,但好像被忽略了。它实际上并没有延迟任何事情,一旦网页加载,它就会给出对象“IWebBrowser2”失败的“方法“文档”错误。页面也有CSS和Java编码,你认为这可能会导致问题吗?
Sub sbIE_OpenVMOSite()

    uname = "username"
    pword = "password"

    myWebsite = "TheWebsite"
    fdUSERNAME = "ContentPlaceHolder1_userid"
    fdPASSWORD = "ContentPlaceHolder1_password"
    fdSUBMIT = "ContentPlaceHolder1_submitthis"

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate myWebsite
    IE.Visible = True
        'Application.Wait DateAdd("s", 5, Now)
    'Do While IE.Busy
    '    Application.Wait DateAdd("s", 5, Now)
    'Loop
    'Wend

    Do
        If IE.readyState = 4 Then
            IE.Visible = False
            Exit Do
        Else
            DoEvents
        End If
    Loop


    'MsgBox ("POP")
    IE.document.getElementByID(fdUSERNAME).Value = uname  'Enters Username
    IE.document.getElementByID(fdPASSWORD).Value = pword  'Enters Password
    IE.document.all(fdSUBMIT).Click

End Sub
Sub sbIE_OpenVMOSite()

    uname = "username"
    pword = "password"

    myWebsite = "http://TheWebsite"
    fdUSERNAME = "ContentPlaceHolder1_userid"
    fdPASSWORD = "ContentPlaceHolder1_password"
    fdSUBMIT = "ContentPlaceHolder1_submitthis"    

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate myWebsite
    IE.Visible = True

    While IE.Busy
        Application.Wait DateAdd("s", 2, Now)
    Wend

    IE.document.getElementById(fdUSERNAME).Value = uname      'Enters Username
    IE.document.getElementById(fdPASSWORD).Value = pword  'Enters Password    
    IE.document.all(fdSUBMIT).Click

End Sub