Jump to content

Open web page in existing IE window and add content


Recommended Posts

I am trying to focus IE, then to navigate to a web page in existing IE window (same tab), and add text to fields.

Here is what I have so far:

#include <IE.au3>

Global $website_link = "https://www.google.com/"
Global $TextToAdd = "Hello there"

Global $IEpath = "C:\Program Files\Internet Explorer\iexplore.exe"
If WinActivate("[CLASS:IEFrame]") Then
    WinSetState("[CLASS:IEFrame]", "", @SW_MAXIMIZE)
Else
    Run ($IEpath , "" , @SW_SHOWMAXIMIZED)
EndIf

$oie = _IEAttach(WinGetHandle("[Active]"), "hwnd")
If @error Then
    Run ($IEpath , "" , @SW_SHOWMAXIMIZED)
EndIf
_IENavigate($oie, $website_link)

sleep(1000)
Global $oTitle = _IEGetObjByName($oIE, "q")
_IEFormElementSetValue($oTitle, $TextToAdd)

Exit

It works a few times then I trigger something (minimized IE, not active, not started etc.) and it doesn't work even if I exit and start the script again. Here is the error message that I am getting:

Quote

 

Line 272  (File "C:\Program Files (x86)\AutoIt3\Include\IE.au3"):

$oObject.navigate($sUrl)
$oObject^ ERROR

Error: The requested action with this object has failed.

 


Help with this code or a new solution would be very helpful.

Link to comment
Share on other sites

Here how I would do it using more of the _IE

#include <Constants.au3>
#include <IE.au3>

Opt ("MustDeclareVars", 1)

Global $website_link = "https://www.google.com/"
Global $TextToAdd = "Hello there", $oIE

$oIE = _IECreate ("about:blank", 1)
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error creating/attaching IE")
Global $hWnd = _IEPropertyGet ($oIE, "hwnd")
If @error Or Not IsHWnd ($hWnd) then Exit MsgBox ($MB_SYSTEMMODAL,"","Error getting hwnd")
WinSetState($hWnd, "", @SW_MAXIMIZE)
WinActivate($hWnd)
WinWaitActive($hWnd)

_IENavigate($oIE, $website_link)
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error navigating IE")

Global $oTitle = _IEGetObjByName($oIE, "q")
_IEFormElementSetValue($oTitle, $TextToAdd)

 

Link to comment
Share on other sites

Thanks Nine,

This line is my "problem", I need to use existing open Internet explorer to open a web page.

$oIE = _IECreate ("about:blank", 1)

My script adds posts to wordpress website and whenever I run the script it opens new IE window which is not acceptable for my project.

Link to comment
Share on other sites

20 minutes ago, prodesigner said:

This line is my "problem", I need to use existing open Internet explorer to open a web page.

The number 1 after url is a TryToAttach.  So if an instance of IE exists it will attach instead of creating a new...

Link to comment
Share on other sites

I see what is the problem, with the _IECreate, now try this :

#include <Constants.au3>
#include <IE.au3>

Opt ("MustDeclareVars", 1)

Global $website_link = "https://www.google.com/"
Global $TextToAdd = "Hello there", $oIE

Global $hWnd = WinGetHandle ("[CLASS:IEFrame]")
If $hWnd Then
  $oIE = _IEAttach ($hWnd, "hwnd")
  If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error attaching IE")
Else
  $oIE = _IECreate ()
  If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error creating IE")
  $hWnd = _IEPropertyGet ($oIE, "hwnd")
EndIf
WinSetState($hWnd, "", @SW_MAXIMIZE)
WinActivate($hWnd)
WinWaitActive($hWnd)

_IENavigate($oIE, $website_link)
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error navigating IE")

Global $oTitle = _IEGetObjByName($oIE, "q")
_IEFormElementSetValue($oTitle, $TextToAdd)

 

Edited by Nine
Link to comment
Share on other sites

Thanks again, I got it to work at first but now I am getting error"

Quote

Line 272  (File "C:\Program Files (x86)\AutoIt3\Include\IE.au3"):

$oObject.navigate($sUrl)
$oObject^ ERROR

Error: The requested action with this object has failed.

It's newest Autoit V3, no modifications. Used Notepad++, then Scite to save the script.

Link to comment
Share on other sites

  • 2 weeks later...

I got a new scenario with the code provided by Nine.

If I have an IE window with a file loaded from local computer (c:/sample.html), new http load will not attach to current window but it will open a new one. Is there a possibility to attach http page to local html file window?

 

Sample HTML:

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

 

Edited by prodesigner
Link to comment
Share on other sites

  • 2 weeks later...

I am still looking for a solution to attach html file from hdd to existing IE, then to attach any www page to the same tab and repeat it x times. The problem is, it will open new window for local html file (check my previous message with a better explanation).

Any advice?

Edited by prodesigner
Link to comment
Share on other sites

Here is the code, sample html attached. The code bellow works good when attaching to www style links, but it is not working when attaching local html file.

 

#include <GUIConstantsEx.au3> ; for gui
#include <MsgBoxConstants.au3> ; for messages
#include <IE.au3> ; for Internet Explorer


Opt ("MustDeclareVars", 1)

Global $hWnd = WinGetHandle ("[CLASS:IEFrame]")
If $hWnd Then
  Global $oIE = _IEAttach ($hWnd, "hwnd")
  If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error attaching IE")
Else
  Global $oIE = _IECreate ()
  If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error creating IE2")
  $hWnd = _IEPropertyGet ($oIE, "hwnd")
EndIf
WinSetState($hWnd, "", @SW_MAXIMIZE)
WinActivate($hWnd)
WinWaitActive($hWnd)

_IENavigate($oIE, "G:\index.html")
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error navigating IE3")


Sleep(2000)


Global $hWnd = WinGetHandle ("[CLASS:IEFrame]")
If $hWnd Then
  Global $oIE = _IEAttach ($hWnd, "hwnd")
  If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error attaching IE")
Else
  Global $oIE = _IECreate ()
  If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error creating IE2")
  $hWnd = _IEPropertyGet ($oIE, "hwnd")
EndIf
WinSetState($hWnd, "", @SW_MAXIMIZE)
WinActivate($hWnd)
WinWaitActive($hWnd)

_IENavigate($oIE, "https://www.autoitscript.com")
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error navigating IE3")


Exit

 

index.html

Link to comment
Share on other sites

Quote

I trust that the path of you local html file is probably wrong.

The path to the file is fine, actually I am working on it hours and hours already, not just today. It's weird that it is working for you. I also tried a lot of other ideas from the forum and nothing worked for me. Here are some conclusions for the code from above:

1. attach www, then local html - pass
2. attach www, then www again - pass
3. attach local html, then local html again - pass
4. attach local html, then www - fail (it opens a new window, not maximized which is also a signal for something)

That #4 is preventing me to repeat the whole action that includes local htmls and www web site.

Edited by prodesigner
Link to comment
Share on other sites

Using the first example from help files:
I found something that might be wrong, apart from the same (new window) behavior like earlier I found this (image attached): file:///G:/index.html  - this /// is weird to me and might be the problem.

I will try all other examples too and hopefully will find out what is the problem.

#include <MsgBoxConstants.au3> ; for messages
#include <IE.au3> ; for Internet Explorer

Opt ("MustDeclareVars", 1)

Global $hWnd = WinGetHandle ("[CLASS:IEFrame]")
If $hWnd Then
  Global $oIE = _IEAttach ($hWnd, "hwnd")
  If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error attaching IE")
Else
  Global $oIE = _IECreate ()
  If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error creating IE2")
  $hWnd = _IEPropertyGet ($oIE, "hwnd")
EndIf
WinSetState($hWnd, "", @SW_MAXIMIZE)
WinActivate($hWnd)
WinWaitActive($hWnd)

_IENavigate($oIE, "G:\index.html")
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error navigating IE3")



Sleep(2000)

Global $oIE = _IEAttach ("HTML attach by title")
MsgBox($MB_SYSTEMMODAL, "The URL", _IEPropertyGet($oIE, "locationurl"))

_IENavigate($oIE, "https://www.autoitscript.com")
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error navigating IE3")


Exit

 

attach.png

index.html

Link to comment
Share on other sites

Here is the most basic example from help file and it doesn't work for me. I am trying to open www page in the same window after local html file. It usually opens www page in new window but this time it will not load it at all. Here is the error message:

Quote

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

 

; Create a browser window and navigate to a website,
; wait 5 seconds and navigate to another
; wait 5 seconds and navigate to another

#include <IE.au3>

Local $oIE = _IECreate("www.autoitscript.com")
Sleep(2000)
_IENavigate($oIE, "G:\index.html")
Sleep(2000)
_IENavigate($oIE, "http://www.autoitscript.com/forum/index.php?showforum=9")

 

index.html

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