Search code examples
internet-explorerautoitinvisible

How to automate without making Internet Explorer visible?


My AutoIt script executes Google search queries. For each one it pulls a conversion ratio for United States dollars to another currency. It displays Internet Explorer, and I do not want it to.

Here's the code:

#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIEdit.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <array.au3>
#include <String.au3>
#include <IE.au3>

$oIE = _IECreate()
_IENavigate($oIE, "https://www.google.com/search?q=convert+us+dollars+to+euros")
$aText = _IEBodyReadText($oIE)
If StringInStr($aText, "equals") Then
    $num_pos = StringInStr($aText, "equals")
    $one_num = StringMid($aText, ($num_pos + 10), 4)
EndIf

_IENavigate($oIE, "https://www.google.com/search?q=convert+us+dollars+to+japanese+yen")
$aText = _IEBodyReadText($oIE)
If StringInStr($aText, "equals") Then
    $num_pos = StringInStr($aText, "equals")
    $two_num = StringMid($aText, ($num_pos + 10), 6)
EndIf

_IENavigate($oIE, "https://www.google.com/search?q=convert+us+dollars+to+british+pounds")
$aText = _IEBodyReadText($oIE)
If StringInStr($aText, "equals") Then
    $num_pos = StringInStr($aText, "equals")
    $three_num = StringMid($aText, ($num_pos + 10), 4)
EndIf

_IENavigate($oIE, "https://www.google.com/search?q=convert+us+dollars+to+aruban+florins")
$aText = _IEBodyReadText($oIE)
If StringInStr($aText, "equals") Then
    $num_pos = StringInStr($aText, "equals")
    $four_num = StringMid($aText, ($num_pos + 10), 4)
EndIf

$res_string = $one_num & "+" & $two_num & "+" & $three_num & "+" & $four_num
MsgBox($MB_OK, "Here are the results", $res_string)

Solution

  • … without Internet Explorer being visible.

    As per Documentation - User Defined Function Reference - _IECreate() :

    $iVisible [optional] specifies whether the browser window will be visible
    0 = Browser Window is hidden
    1 = (Default) Browser Window is visible

    Example :

    #include <IE.au3>
    
    Global Const $g_sUrl     = 'https://stackoverflow.com/'
    Global Const $g_iAttach  = 0
    Global Const $g_iVisible = 0
    Global Const $g_iWait    = 1
    Global Const $g_iFocus   = 0
    
    Global       $g_oIE      = _IECreate($g_sUrl, $g_iAttach, $g_iVisible, $g_iWait, $g_iFocus)
    Global       $g_sPage    = _IEBodyReadText($g_oIE) & @CRLF
    
    ConsoleWrite($g_sPage)
    _IEQuit($g_oIE)