Jump to content

how to Auto submit IE page form with Javascript?


Recommended Posts

Hi All

#include <IE.au3>

Dim $username = "aaa"

Dim $password = "123456"

$oIE = _IECreate ("http://www.abc.com",0,1,1,0)

$o_login = _IEGetObjById ($oIE, "u")

$o_password = _IEGetObjById ($oIE, "p")

_IEFormElementSetValue ($o_login, $username)

_IEFormElementSetValue ($o_password, $password)

$oIE.document.parentwindow.execScript('void(0);')

A:tired use follwoing to set value for password and username.

but seems nothing work.

_IEGetObjByname,

B: I could not get the logon button work either be clicked either.

tired lock by URL,TEXT,button,IMG, name

C: do not want use mouse(xxx,yyy) or keyboard(tab focus)to solve this

I checked almost all auto login relate to Javascript in this forum nothing close.

Really appreciate your help thanks.

;---------------Here are the target code in different file-----------------------------

IN xhtml

;<a id="aLogin" class="btn_login" href="javascript:void(0);"><span>login</span></a>

;-------------------------------------------------------------------------------------

IN login.js

var u, p, c;

$(function () {

u = $("#u"); p = $("#p"); c = $("#c");

$("#aRecode").click(function () {

$("#vcImg").attr("src", csdn.vc_handler + "?r=" + Math.random());

return false;

});

$("#aLogin").click(function () {

singin();

return false;

});

c.focus(function () {

if ($("#tr_vc").css("display") == "none") {

$("#tr_vc").show();

try { csdn.parent.document.getElementById("logfrm").style.height = "340px"; }

catch (err) { }

}

});

u.keydown(function (ev) {

ev = ev || event;

if (csdn.isEnter(ev)) singin();

});

p.keydown(function (ev) {

ev = ev || event;

if (csdn.isEnter(ev)) singin();

});

c.keydown(function (ev) {

ev = ev || event;

if (csdn.isEnter(ev)) singin();

});

$("#chkRemember").keydown(function (ev) {

ev = ev || event;

if (csdn.isEnter(ev)) singin();

});

showVC();

..................

Link to comment
Share on other sites

Did you try _IELinkClickByText($oIE, "login")

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Did you try _IELinkClickByText($oIE, "login")

Dale

Hi Dale

Thanks for reply this post. As I read a lot posts before was sloved by you.

honestly I did not try "_IELinkClickByText"because in sample it need a link and a text to make it work.

such as <a href='http://www.autoitscript.com/forum/'>user forum</a>

in my case neither exsit.

i use iebuilder and debugbar load that login page.

debugbar show me more detail than iebuilder as below

****************************************************************

<IFRAME id=logfrm style="WIDTH: 400px; HEIGHT: 230px" src="/account/loginbox?callback=logined" frameBorder=0 scrolling=no></IFRAME>

......

<FORM name=logform action=#><TABLE>

......

<TD><INPUT class=inputbox id=u maxLength=100 value=aaa name=u jQuery1314434233484="8"></TD></TR>

...

<TD><INPUT class=inputbox id=p type=password maxLength=50 value="" jQuery1314434233484="10"></TD></TR>

...

<TD><A class=btn_login id=aLogin href="javascript:void(0);" jQuery1314434233484="4"><SPAN>login</SPAN></A>

****************************************************************

accroding to the code I think I should be able to get and set the value to "u"&"p"

by using _IEGetObjById and _IEFormElementSetValue

But I could not verify it and I can not execuate "javascript:void(0)" either

which is the submite button. text is seperated in a css file.

void function Js script is login.js I had post above.

I am new to autoit(2 weeks only) sorry about my naive question.

Is this something to do with DOM(not sure what it is) or running java scrip by autoit. I think i lost.

but I feel these two posts are close to my question somehow.

$oIE.document.parentwindow.execScript('DoSomething(1,22);')

(in my case how can i pass the parametter or assign value to a function on webpage)

at last really appreciate your help.

Link to comment
Share on other sites

Based on the HTML you posted: ;<a id="aLogin" class="btn_login" href="javascript:void(0);"><span>login</span></a>

I would expect _IELinkClickByText($oIE, "login") to work.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

  • Moderators

Based on the HTML you posted: ;<a id="aLogin" class="btn_login" href="javascript:void(0);"><span>login</span></a>

I would expect _IELinkClickByText($oIE, "login") to work.

Dale

If that's not working, this might:

(pseudo code: )

Global $go_IE = ; however you're getting the IE object

Global $go_LoginBtn = _IEGetObjById($go_IE, "aLogin")
If Not IsObj($go_LoginBtn) Then Exit 1

_IEAction($go_LoginBtn, "click")
Sleep(250)
_IELoadWait($go_IE)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

thanks for both of you

tired neither work. Seems _IEFormGetObjByName and _IEGetObjById could not get the value.

--> IE.au3 V2.4-0 Warning from function _IEFormGetObjByName, $_IEStatus_NoMatch

--> IE.au3 V2.4-0 Warning from function _IEGetObjById, $_IEStatus_NoMatch (u)

--> IE.au3 V2.4-0 Warning from function _IEGetObjById, $_IEStatus_NoMatch (p)

--> IE.au3 V2.4-0 Error from function _IEFormElementSetValue, $_IEStatus_InvalidDataType

--> IE.au3 V2.4-0 Error from function _IEFormElementSetValue, $_IEStatus_InvalidDataType

--> IE.au3 V2.4-0 Warning from function _IEGetObjById, $_IEStatus_NoMatch (aLogin)

>Exit code: 1 Time: 11.587

-----------------------------------------

here is my code

Dim $username = "aaa"

Dim $password = "bbb"

Global $go_IE = _IECreate ("http://www.aaa.com/login",0,1,1,0)

$oForm = _IEFormGetObjByName ($go_IE, "logform")

$o_login = _IEGetObjById ($go_IE, "u")

$o_password = _IEGetObjById ($go_IE, "p")

_IEFormElementSetValue ($o_login, $username)

_IEFormElementSetValue ($o_password, $password)

;_IELinkClickByText($oIE, "login")

Global $go_LoginBtn = _IEGetObjById($go_IE, "aLogin")

If Not IsObj($go_LoginBtn) Then Exit 1

_IEAction($go_LoginBtn, "click")

Sleep(250)

_IELoadWait($go_IE)

Link to comment
Share on other sites

  • Moderators

You've reached frustration point on your part I'm sure, but more on the part of those that can help you.

If the data you've provide is true, then other than maybe an IE-9 issue, you shouldn't be having any problems.

Of course, we know the URL(s) are fiction, so there's little that can be done on our part to actually help you, other than trust you semi know what you're doing.

Try this and work from the point of the error:

#include <IE.au3>

Global $gs_Username = "aaa"
Global $gs_Password = "bbb"
Global $gs_URL = "http://www.aaa.com/login"

Global $go_IE = _IECreate($gs_URL)
If Not IsObj($go_IE) Then
    MsgBox(16 + 262144, "Error", "Could not create IE object")
    Exit 1
EndIf
Sleep(5000) ; sleep 5 seconds just for giggles

Global $go_Form = _IEGetObjByName($go_IE, "logform")
If Not IsObj($go_Form) Then
    MsgBox(16 + 262144, "Error", "Could not get the Form object")
    Exit 2
EndIf

Global $go_login = _IEGetObjById($go_Form, "u")
If Not IsObj($go_login) Then
    MsgBox(16 + 262144, "Error", "Could not get login object")
    Exit 3
EndIf

Global $go_password = _IEGetObjById ($go_Form, "p")
If Not IsObj($go_password) Then
    MsgBox(16 + 262144, "Error", "Could not get password object")
    Exit 4
EndIf

_IEFormElementSetValue ($go_login, $gs_Username)
_IEFormElementSetValue ($go_password, $gs_Password)

Global $go_LoginBtn = _IEGetObjById($go_Form, "aLogin")
If Not IsObj($go_LoginBtn) Then
    MsgBox(16 + 262144, "Error", "Could not get login object")
    Exit 5
EndIf

_IEAction($go_LoginBtn, "click")
Sleep(250)
_IELoadWait($go_IE)
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks SmOke_N. My testing environment is IE7.

here is the result for running your code.

Error msg ”Could not get the Form object“

PS: I could not detect the form from iebuilder either.

But I can see it from Debugbar.

because the site is not in English so I did not give it out at begining.

I understand it would be very hard to provide help in that way.

That is why I am really thanks all of you.

here is the site http://passport.csdn.net/account/login

--------------------------------------------------------------------------------

running result:

--> IE.au3 V2.4-0 Warning from function _IELoadWait, $_IEStatus_LoadWaitTimeout

--> IE.au3 V2.4-0 Warning from function _IEGetObjByName, $_IEStatus_NoMatch (Name: logform, Index: 0)

>Exit code: 2 Time: 521.047

Link to comment
Share on other sites

  • Moderators

I believe IE.au3 only works on the html data that has been finalized.

Working with a frame, you have to make sure the data you're trying to manipulate has everything you're looking for.

Here's one way to see ( look for your inputs ( user/pass) and your submit button data ).

#include <IE.au3>

Global $gs_URL = "http://passport.csdn.net/account/login"

; switch back to _IECreate() if you don't already have that login page open in IE
Global $go_IE = _IEAttach($gs_URL, "url");_IECreate($gs_URL)
If Not IsObj($go_IE) Then
    MsgBox(16 + 262144, "Error", "Could not create IE object")
    Exit 1
EndIf

Global $go_Frame = _IEGetObjById($go_IE, "logfrm")
If Not IsObj($go_Frame) Then
    MsgBox(16 + 262144, "Error", "Could not create Frame object")
    Exit 2
EndIf

ConsoleWrite(_IEDocReadHTML($go_Frame) & @CRLF)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

But for a typo...

Global $go_Frame = _IEGetObjById($go_IE, "logfrm")

needs to be

Global $go_Frame = _IEFrameGetObjByName($go_IE, "logfrm")

Then to click the login link, use _IELinkBClickByText($go_Frame, "登 录")

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

  • Moderators

Oops and Good point Dale, but that works:

#include <IE.au3>

Global $gs_Username = "aaa"
Global $gs_Password = "bbb"
Global $gs_URL = "http://passport.csdn.net/account/login"

Global $go_IE = _IEAttach($gs_URL, "url");_IECreate($gs_URL)
If Not IsObj($go_IE) Then
    MsgBox(16 + 262144, "Error", "Could not create IE object")
    Exit 1
EndIf

Global $go_Frame = _IEFrameGetObjByName($go_IE, "logfrm")
If Not IsObj($go_Frame) Then
    MsgBox(16 + 262144, "Error", "Could not create Frame object")
    Exit 2
EndIf

Global $go_Form = _IEGetObjByName($go_Frame, "logform")
If Not IsObj($go_Form) Then
    MsgBox(16 + 262144, "Error", "Could not get the Form object")
    Exit 3
EndIf

Global $go_login = _IEGetObjById($go_Form, "u")
If Not IsObj($go_login) Then
    MsgBox(16 + 262144, "Error", "Could not get login object")
    Exit 4
EndIf

Global $go_password = _IEGetObjById ($go_Form, "p")
If Not IsObj($go_password) Then
    MsgBox(16 + 262144, "Error", "Could not get password object")
    Exit 5
EndIf

_IEFormElementSetValue ($go_login, $gs_Username)
_IEFormElementSetValue ($go_password, $gs_Password)

Global $go_LoginBtn = _IEGetObjById($go_Form, "aLogin")
If Not IsObj($go_LoginBtn) Then
    MsgBox(16 + 262144, "Error", "Could not get login object")
    Exit 6
EndIf

_IEFormSubmit($go_Form)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Hi SmOke_N

I had run your code. you are right. there is no inputs for( user/pass)info in form "logfrm"

only thing relate to it is this line

<DIV class=left_login><IFRAME id=logfrm style="WIDTH: 400px; HEIGHT: 230px" src="/account/loginbox?callback=logined" frameBorder=0 scrolling=no></IFRAME></DIV>

I can see this from Google Chrome inspect element as well.

But strange I see another form from "IE debug bar" Or "view source in IE" which have inputs for(user/pass) named as "logform" I think that is the finalized code on client browser.

When I try use logform on your code it tell me "Could not create Frame object" So I guess that from is generate dynamics by one of JS. (IN first post login.js PS. there are 5 more js in this page which i did not list here)

Link to comment
Share on other sites

  • Moderators

The last piece of code I posted in post #11, you'll see I said "It works", which means I tested it and know it does.

Look at _IEAttach() and _IECreate(), make sure you're using the right one ( _IEAttach if the window is already open ), maybe that's your issue.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

But for a typo...

Global $go_Frame = _IEGetObjById($go_IE, "logfrm")

needs to be

Global $go_Frame = _IEFrameGetObjByName($go_IE, "logfrm")

Then to click the login link, use _IELinkBClickByText($go_Frame, "登 录")

Dale

Thanks Dale.

Strange both _IEFrameGetObjByName and _IEGetObjById work on SmOke_N's code.

as mentioned in prev post "登 录" is not showing in the logfrm frame.

May be I confused both of you I did not try to click on the URL link on top right page

the organe button is.

post-66911-0-28296800-1314578169_thumb.j

Link to comment
Share on other sites

The last piece of code I posted in post #11, you'll see I said "It works", which means I tested it and know it does.

Look at _IEAttach() and _IECreate(), make sure you're using the right one ( _IEAttach if the window is already open ), maybe that's your issue.

Hi Smoke

I tested Both _IEAttach() and _IECreate() can show me the content of the page.

but there is not inputs I am looking for (username ,password and click buttom)

Link to comment
Share on other sites

  • Moderators

Run that code, (Chang to _IECreate one if you don't have the login page visible already ) comment out _IEFormSubmit() you will see your data filled.

It's the wrong username and password, so it resets the controls if you don't uncomment _IEFormSubmit().

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Run that code, (Chang to _IECreate one if you don't have the login page visible already ) comment out _IEFormSubmit() you will see your data filled.

It's the wrong username and password, so it resets the controls if you don't uncomment _IEFormSubmit().

Thanks SmOke_N

with _IEFormSubmit() comments

I can see the form is filled with correct information(username and password)and when I physical click on the button I can login it.

But with _IEFormSubmit() uncommented the form was cleared.

PS: aaa and bbb is not real username and password.

I feel we close to the answer:)

Link to comment
Share on other sites

Hi All

i have found a work around :mellow: use send ("{ENTER}") to submit the page after form filled.

I think it is only work on the page that support hotkey and auto focus on submit button.

not sure why _IEFormSubmit() not working in this case.

I have tried following line not work either.

;$oIE.document.parentwindow.execscript("void(0)")

it would be nice to know how to submit it correctly.

Hi SmOke_N and Dale

Just want to say thanks again for all your time on this post

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...