当按钮没有名称/ ID时,单击带有VBA的JS按钮

编程入门 行业动态 更新时间:2024-10-26 05:25:26
本文介绍了当按钮没有名称/ ID时,单击带有VBA的JS按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我无法为我的生活找出如何用VBA点击下面的按钮。任何帮助将不胜感激。我已经能够填写用户名/密码字段,因为它们有一个名称,所以我使用getElementsByName,但按钮没有名称或ID。

具体按钮代码是:

< table class =button>< tr>< td>< div class =button-left>< input type =submitclass =form-b​​uttonvalue =Submit>

以下是完整的脚本/表单代码,以帮助您

< form name =loginFormBeanmethod =postaction =/ XXXXXXXX / login.doonsubmit =return validateForm(this)> < TABLE border =0cellpadding =5cellspacing =0> < TR> < TD class =bigGreyContentnowrap>用户ID< / TD> < TD class =bigGreyContentalign =left> < input type =textname =usernamemaxlength =50size =40value =class =bgGreenColor> < / TD> < / TR> < TR> < TD class =bigGreyContentnowrap>密码< / TD> < TD class =bigGreyContentalign =left> < input type =passwordname =passwordmaxlength =50size =40value =class =bgGreenColor> < / TD> < / TR> < TR> < TD colspan =2align =center> < BR> < table class =button>< tr>< td>< div class =button-left>< input type =submitclass =form-b​​utton =提交>< / div>< div class =button-right>< / div>< / td>< / tr>< / table> < / TD> < / TR> < / TABLE> < / form>

编辑:启用了以下引用(除了默认值):

Microsoft HTML对象库 Microsoft Internet控件 Microsoft WinHTTP服务版本5.1 Microsoft XML,v6.0

我目前的代码是:

Sub XXXX() Dim objIE As InternetExplorer Dim http As New MSXML2.XMLHTTP60 Dim html As New HTMLDocument Dim btn As Object Set objIE = New InternetExplorer Set btn = html.getElementsByClassName( button-left)(0).getElementsByTagName(input)(0) objIE.Visible = True objIE.navigatehttp://XXXX/login.jsp Do While objIE.Busy = True或objIE.readyState<> 4:DoEvents:Loop objIE.document.getElementsByName(username)(0).Value =XXXX objIE.document.getElementsByName(password)(0).Value =YYYY btn.Click End Sub

解决方案

假设class =button-left是您的html中第一次出现,请尝试:

Dim btn As Object 设置btn = html.getElementsByClassName(button-left)(0).getElementsByTagName(input)(0) btn.Click

编辑

您应该添加 Microsoft HTML对象库和 Microsoft XML v6.0 如果您还没有,从工具 - > ;参考。

目前你被困在你的方法和我的方法之间。您需要选择一个,请尝试这样:

Sub XXXX() Dim http As New MSXML2.XMLHTTP60 Dim html作为新的HTMLDocument Dim btn,usr,psw As Object 使用CreateObject(MSXML2.serverXMLHTTP)。打开GET,http: //XXXX/login.jsp,False .send html.body.innerHTML = .responseText 结束 设置usr = html.getElementsByTagName(表单)(0).getElementsByTagName(input)(0)设置psw = html.getElementsByTagName(Form)(0).getElementsByTagName(input)(1)设置btn = html.getElementsByTagName(Form)(0).getElementsByTagName(input)(2) usr.Value =用户名 psw.Value =密码 btn.click Set html = Nothing:Set btn = Nothing:Set usr = Nothing:Set psw = Nothing End Sub / pre>

I can't for the life of me figure out how to click the button below with VBA. Any help would be appreciated. I've been able to populate the username/password fields since they have a name so I use getElementsByName but the button doesn't have a name or ID.

The specific button code is:

<table class="button"><tr><td><div class="button-left"><input type="submit" class="form-button" value="Submit" >

Here is the full script/form code in case it helps

<form name="loginFormBean" method="post" action="/XXXXXXXX/login.do" onsubmit="return validateForm(this)"> <TABLE border="0" cellpadding="5" cellspacing="0"> <TR> <TD class="bigGreyContent" nowrap>User ID</TD> <TD class="bigGreyContent" align="left"> <input type="text" name="username" maxlength="50" size="40" value="" class="bgGreenColor"> </TD> </TR> <TR> <TD class="bigGreyContent" nowrap>Password</TD> <TD class="bigGreyContent" align="left"> <input type="password" name="password" maxlength="50" size="40" value="" class="bgGreenColor"> </TD> </TR> <TR> <TD colspan="2" align="center"> <BR> <table class="button"><tr><td><div class="button-left"><input type="submit" class="form-button" value="Submit" ></div><div class="button-right"></div></td></tr></table> </TD> </TR> </TABLE> </form>

EDIT: I have the following references enabled (in addition to the default ones):

Microsoft HTML Object Library Microsoft Internet Controls Microsoft WinHTTP Services, Version 5.1 Microsoft XML, v6.0

My current code is:

Sub XXXX() Dim objIE As InternetExplorer Dim http As New MSXML2.XMLHTTP60 Dim html As New HTMLDocument Dim btn As Object Set objIE = New InternetExplorer Set btn = html.getElementsByClassName("button-left")(0).getElementsByTagName("input")(0) objIE.Visible = True objIE.navigate "XXXX/login.jsp" Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop objIE.document.getElementsByName("username")(0).Value = "XXXX" objIE.document.getElementsByName("password")(0).Value = "YYYY" btn.Click End Sub

解决方案

Assuming class="button-left" is the first occurrence within your html, try this:

Dim btn As Object Set btn = html.getElementsByClassName("button-left")(0).getElementsByTagName("input")(0) btn.Click

Edit:

You should add Microsoft HTML Object Library and Microsoft XML v6.0 if you haven't already, from Tools -> References.

Currently you are stuck between your method and my method. You need to pick one so please try this:

Sub XXXX() Dim http As New MSXML2.XMLHTTP60 Dim html As New HTMLDocument Dim btn, usr, psw As Object With CreateObject("MSXML2.serverXMLHTTP") .Open "GET", "XXXX/login.jsp", False .send html.body.innerHTML = .responseText End With Set usr = html.getElementsByTagName("Form")(0).getElementsByTagName("input")(0) Set psw = html.getElementsByTagName("Form")(0).getElementsByTagName("input")(1) Set btn = html.getElementsByTagName("Form")(0).getElementsByTagName("input")(2) usr.Value = "Username" psw.Value = "Password" btn.click Set html = Nothing: Set btn = Nothing: Set usr = Nothing: Set psw = Nothing End Sub

更多推荐

当按钮没有名称/ ID时,单击带有VBA的JS按钮

本文发布于:2023-06-09 04:10:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/594956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   单击   名称   ID   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!