Jump to content

Help with enter text in Internet Explorer


 Share

Recommended Posts

Please help me. I am very new to Autoit and am struggling with some code. I am trying to write a script to automatically log into my router and change the isp details. I need to make this change twice a day and plan to create two exe files which will run with windows task scheduler at the appropriate times. I am 90% there but can't seem to get the new username and password to enter in the fields. My code is:

$oIE = _IECreate ("http://10.0.0.2/login_security.html")

_IELoadWait($oIE,0)
Sleep(5000)

$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)

$oIECollection = _IEFormGetCollection($oIE, 0)

$oLogin_NameField = _IEFormElementGetObjByName($oIECollection, 'Login_Name')

_IEFormElementSetValue($oLogin_NameField, "admin")

$oLogin_PwdField = _IEFormElementGetObjByName($oIECollection, 'Login_Pwd')
_IEFormElementSetValue($oLogin_PwdField, "admin")

Send("{ENTER}")

_IELoadWait($oIE,0)
Sleep(5000)

MouseClick("",850, 140,1)

_IELoadWait($oIE,0)
Sleep(5000)

$oIECollection = _IEFormGetCollection($oIE, 0)

$owan_PPPUsernameField = _IEFormElementGetObjByName($oIECollection, 'wan_PPPUsername')

_IEFormElementSetValue($owan_PPPUsernameField, "testname")

It works perfectly until the last two lines. If I run it the login to the router works and the appropriate page opens with the isp details but that is as far as it gets and autoit shows an error:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "F:\tp link\test\test.au3"    
--> IE.au3 T3.0-2 Warning from function _IEFormElementGetObjByName, $_IESTATUS_NoMatch
--> IE.au3 T3.0-2 Error from function _IEFormElementSetValue, $_IESTATUS_InvalidDataType
>Exit code: 0    Time: 17.25

When I right click on the input box for the username it shows the name as "wan_PPPUsername" but for some reason it looks like autoit does not find a match for it. I have tried everything I can think of and read so many help files but I am still getting nowhere. Most grateful if anyone can point me in the right direction.

Link to comment
Share on other sites

On 14/10/2016 at 7:06 AM, GeorgeSA said:

I suspect I need to tell autoit to look within these frames but I don't know how to do that.

_IEFrameGetObjByName

then use the element you get with

$oFrame = _IEFrameGetObjByName($oIE, "FrameName")
_IELoadWait($oFrame)
$oFrameDocument = $oFrame.document
$owan_PPPUsernameField = _IEFormElementGetObjByName($oFrameDocument, 'wan_PPPUsername')
;if the above line fails, try:
;$owan_PPPUsernameField = $oFrameDocument.getElementsByName('wan_PPPUsername')
;If $owan_PPPUsernameField.length > 0 Then $owan_PPPUsernameField(0).value = "testname"

 

Edited by genius257
my script did not always work, so added optional code that worked in my tests
Link to comment
Share on other sites

I tried your script but no luck then I realised that I had given you wrong information. The box I am trying to input to is in a FORM called "Alpha_WAN" not a frame. I then tried using

$oFrame = _IEFormGetObjByName($oIE, "Alpha_WAN")

but this gave an error:

--> IE.au3 T3.0-2 Warning from function _IEFormGetObjByName, $_IESTATUS_NoMatch

Then tried

$oFrame = _IEFormElementGetObjByName($oIE, "Alpha_WAN")

gave error:

--> IE.au3 T3.0-2 Error from function _IEFormElementGetObjByName, $_IESTATUS_InvalidObjectType

I also tried both of the above with the name "wan_PPPUsername" but with the same results.

I would be grateful for any further help.

Link to comment
Share on other sites

4 hours ago, GeorgeSA said:

The box I am trying to input to is in a FORM called "Alpha_WAN" not a frame.

So the form you are trying to access is not within an iframe?

NVM. after seeing your recent post i know the answer is yes it is within a frame.

4 hours ago, GeorgeSA said:

Then tried

$oFrame = _IEFormElementGetObjByName($oIE, "Alpha_WAN")

gave error:

--> IE.au3 T3.0-2 Error from function _IEFormElementGetObjByName, $_IESTATUS_InvalidObjectType

 

try:

$oFrame = _IEFrameGetObjByName($oIE, "main")
_IELoadWait($oFrame)
$oFrameDocument = $oFrame.document
$owan_PPPUsernameField = $oFrameDocument.getElementsByName('wan_PPPUsername')
If $owan_PPPUsernameField.length > 0 Then $owan_PPPUsernameField(0).value = "testname"

 

Link to comment
Share on other sites

14 minutes ago, genius257 said:

So the form you are trying to access is not within an iframe?

NVM. after seeing your recent post i know the answer is yes it is within a frame.

 

try:

$oFrame = _IEFrameGetObjByName($oIE, "main")
_IELoadWait($oFrame)
$oFrameDocument = $oFrame.document
$owan_PPPUsernameField = $oFrameDocument.getElementsByName('wan_PPPUsername')
If $owan_PPPUsernameField.length > 0 Then $owan_PPPUsernameField(0).value = "testname"

 

That gives:

--> IE.au3 T3.0-2 Warning from function _IEFrameGetObjByName, $_IESTATUS_NoMatch (No Frames found)
--> IE.au3 T3.0-2 Error from function _IELoadWait, $_IESTATUS_InvalidDataType
"F:\tp link\test\test.au3" (54) : ==> Variable must be of type "Object".:
$oFrameDocument = $oFrame.document
$oFrameDocument = $oFrame^ ERROR

Link to comment
Share on other sites

17 minutes ago, GeorgeSA said:

That gives:

--> IE.au3 T3.0-2 Warning from function _IEFrameGetObjByName, $_IESTATUS_NoMatch (No Frames found)
--> IE.au3 T3.0-2 Error from function _IELoadWait, $_IESTATUS_InvalidDataType
"F:\tp link\test\test.au3" (54) : ==> Variable must be of type "Object".:
$oFrameDocument = $oFrame.document
$oFrameDocument = $oFrame^ ERROR

It seems something is wrong when getting the frame. As the page inspector code is shown, that should not happen... Could you show what is in the head element on the page? i fear JS is the culprit.

Link to comment
Share on other sites

Not sure if this is what you want

<html>
<head>
<title>
</title><meta http-equiv="Content-Type" content="text/html; charset=
iso-8859-1">
<meta http-equiv=Content-Script-Type content=text/javascript>
<meta http-equiv=Content-Style-Type content=text/css>
<script>
// this section of script must be at the bottom of the head tag.
//for window
function addLoadEvent(wnd,func)
{
if ("function" == typeof(wnd.onload))
{
var oldloadfunc = wnd.onload;
wnd.onload = function ()
{
oldloadfunc();
func();
}
}
else
{
wnd.onload = func;
}
}
//for frame
function attachLoadEvent(wnd,func)
{
if (wnd.addEventListener)
{
wnd.addEventListener("load", func);
}
else if (wnd.attachEvent)
{
wnd.attachEvent("onload", func);
}
}
function modifyAlignment()
{
var bodywidth = window.frames["header"].document.body.clientWidth;
var body = window.frames["main"].document.body;
body.style.left = (bodywidth - 760)/2;
body.style.position = "absolute";
}
function loadFunc()
{
if ("Microsoft Internet Explorer" != navigator.appName)
{
modifyAlignment();//first for sys load.
addLoadEvent(document.getElementsByName("main")[0], modifyAlignment);//second for GUI refresh.
window.onresize = modifyAlignment;// third for change the web size.
}
}
addLoadEvent(window, loadFunc);
</script>
</head><frameset rows="65,75,*" framespacing="0" border="0" frameborder="0">
<frame name="header" noresize src="status.html" marginwidth="0" marginheight="0">
<frame name="navigation" noresize src="navigation-status.html" marginwidth="0" marginheight="0">
<frame name="main" noresize src="../status/status_deviceinfo.htm" marginwidth="0" marginheight="0">
</frameset><noframes>
</noframes>
</html>

 

Link to comment
Share on other sites

Here you go

$oIE = _IECreate ("http://10.0.0.2/login_security.html")
_IELoadWait($oIE,0)
Sleep(5000)

$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)

$oIECollection = _IEFormGetCollection($oIE, 0)

$oLogin_NameField = _IEFormElementGetObjByName($oIECollection, 'Login_Name')

_IEFormElementSetValue($oLogin_NameField, "admin")

$oLogin_PwdField = _IEFormElementGetObjByName($oIECollection, 'Login_Pwd')
_IEFormElementSetValue($oLogin_PwdField, "admin")

Send("{ENTER}")

_IELoadWait($oIE,0)
Sleep(5000)



MouseClick("",850, 140,1)


_IELoadWait($oIE,0)
Sleep(5000)


$oFrame = _IEFrameGetObjByName($oIE, "main")
_IELoadWait($oFrame)
$oFrameDocument = $oFrame.document
$owan_PPPUsernameField = $oFrameDocument.getElementsByName('wan_PPPUsername')
If $owan_PPPUsernameField.length > 0 Then $owan_PPPUsernameField(0).value = "testname"

Result:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "F:\tp link\test\test.au3"    
--> IE.au3 T3.0-2 Warning from function _IEFrameGetObjByName, $_IESTATUS_NoMatch (No Frames found)
--> IE.au3 T3.0-2 Error from function _IELoadWait, $_IESTATUS_InvalidDataType
"F:\tp link\test\test.au3" (53) : ==> Variable must be of type "Object".:
$oFrameDocument = $oFrame.document
$oFrameDocument = $oFrame^ ERROR
>Exit code: 1    Time: 16.63

 

Link to comment
Share on other sites

2 hours ago, GeorgeSA said:

MouseClick("",850, 140,1)

Why do you use mouseclick and not _IEAction?

And Send could be replaced with _IEFormSubmit.

Also what purpose does the sleep have when you use _IELoadWait?

 

If you think no sensitive data is on the page you could run the code below, and upload the file

$hFile = FileOpen("sHTML.txt", 2)
FileWrite($hFile, $oIE.document.body.innerHTML)
FileClose($hFile)

And if the frames appear in this file I'll be getting confused :/

Link to comment
Share on other sites

Quote

Why do you use mouseclick and not _IEAction?

I tried to use controlclick rather than mouse click but couldn't get it to work so just settled for mouseclick to get past that bit. If I can get the rest of the code to work then I will go back to that bit and try to get rid of the mouseclick for something more elegant.

Quote

And Send could be replaced with _IEFormSubmit.

I just used the first thing I came across that worked.

Quote

Also what purpose does the sleep have when you use _IELoadWait?

Sometimes the page is slow to load from the router so I just put that in for a bit of safety. Once I get the whole thing working then I will take the sleeps out.

I ran the code and the file is attached. Looks like the file relates to the router login page rather than the isp login?

sHTML.txt

Link to comment
Share on other sites

Dang man.  Looks like you have quite a struggle on your hands.  I don't know if this will help, but I always recommend it to those struggling with IE automation.  There is a tool in the downloads section that breaks down the elements of the pages and essentially gives you all the info you normally would need.  I hope it helps you find a solution.

IE Builder

Link to comment
Share on other sites

4 hours ago, GeorgeSA said:

I ran the code and the file is attached. Looks like the file relates to the router login page rather than the isp login?

And did you run the code after the previous code you use that works? like so:

$oIE = _IECreate ("http://10.0.0.2/login_security.html")

_IELoadWait($oIE,0)
Sleep(5000)

$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)

$oIECollection = _IEFormGetCollection($oIE, 0)

$oLogin_NameField = _IEFormElementGetObjByName($oIECollection, 'Login_Name')

_IEFormElementSetValue($oLogin_NameField, "admin")

$oLogin_PwdField = _IEFormElementGetObjByName($oIECollection, 'Login_Pwd')
_IEFormElementSetValue($oLogin_PwdField, "admin")

Send("{ENTER}")

_IELoadWait($oIE,0)
Sleep(5000)

MouseClick("",850, 140,1)

_IELoadWait($oIE,0)
Sleep(5000)

$hFile = FileOpen("sHTML.txt", 2)
FileWrite($hFile, $oIE.document.body.innerHTML)
FileClose($hFile)

 

4 hours ago, GeorgeSA said:

I tried to use controlclick rather than mouse click but couldn't get it to work so just settled for mouseclick to get past that bit. If I can get the rest of the code to work then I will go back to that bit and try to get rid of the mouseclick for something more elegant.

4 hours ago, GeorgeSA said:

I just used the first thing I came across that worked.

4 hours ago, GeorgeSA said:

Sometimes the page is slow to load from the router so I just put that in for a bit of safety. Once I get the whole thing working then I will take the sleeps out.

Fair enough :)

Link to comment
Share on other sites

6 hours ago, MattHiggs said:

Dang man.  Looks like you have quite a struggle on your hands.  I don't know if this will help, but I always recommend it to those struggling with IE automation.  There is a tool in the downloads section that breaks down the elements of the pages and essentially gives you all the info you normally would need.  I hope it helps you find a solution.

Thanks for your suggestion. I will give it a try to see if I can figure anything out.

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...