简体   繁体   English

如何点击这个javascript按钮?

[英]how to click this javascript button?

In the middle of a webpage I am trying to log into I have the following code 在我试图登录的网页中间,我有以下代码

   <tr>
   <td colspan="2" align="center"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">

       <br>
       <a href="javascript:SubmitAction()">
          <IMG SRC="images/logon.gif" WIDTH="47" HEIGHT="43" NATURALSIZEFLAG="3" BORDER="0">
       </a>


   <!--mstheme--></font></td>

I want to execute the SubmitAction() code, which will take my credentials and log me in. Using the WebBrowser from .NET works fine, because I can use "invokescript". 我想执行SubmitAction()代码,它将获取我的凭据并登录。使用.NET中的WebBrowser工作正常,因为我可以使用“invokescript”。 I cannot do this with shdocvw though. 我不能用shdocvw这样做。 How can I click or otherwise make this action occur, especially since the element has no id or tag? 如何单击或以其他方式执行此操作,尤其是因为元素没有id或标记?

Here's what my function currently looks like: 这是我的功能目前的样子:

        private void webBrowser1_DocumentCompleted(object pDisp, ref object URL)
    {
        while (webBrowser.ReadyState < SHDocVw.tagREADYSTATE.READYSTATE_LOADED) { }
        //we are attempting to log in
        if (loggingIn)
        {
            mshtml.HTMLDocumentClass doc = webBrowser.Document as mshtml.HTMLDocumentClass;                
            doc.getElementById("Username").setAttribute("value", "MLAPAGLIA");
            doc.getElementById("Password").setAttribute("value", "PASSWORD");

            mshtml.IHTMLWindow2 win = doc.parentWindow as mshtml.IHTMLWindow2;
            win.execScript("SubmitAction()", "javascript");

            loggingIn = false;
            return;
        }
<a href="#" onclick="SubmitAction();return(false);">

This is not the ideal way to do this, its a better pracitce to use an event listenr.. 这不是理想的做法,它是使用事件监听器的更好的实践。

In JQuery it would be 在JQuery中它会是

    <a href="#" id="someid">

$('#someid').bind("click", function() { .. do whatever });

That way your not entagling your code logic and your sematic markup. 这样你就不会纠缠你的代码逻辑和你的语义标记。

try (EDIT after comment): 尝试(评论后编辑):

using SHDocVw;
using mshtml;


SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
ie.Visible = true;
object o = new object();
ie.Navigate("http://www.google.com", ref o, ref o, ref o, ref o);

while (!(ie.ReadyState >= tagREADYSTATE.READYSTATE_LOADED))
       Application.DoEvents();

var doc = ie.Document;

var win = (IHTMLWindow2)doc.parentWindow;
// here you call the Javascript
win.execScript("SubmitAction();", "javascript");

some interesting links: 一些有趣的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM