Jump to content

Selecting an Option box with IE.au3


NELyon
 Share

Recommended Posts

I'm working on a translator that uses altavista (Thanks AzKay for the idea) and it won't work correctly without selecting an option box. Heres what i'm trying right now:

#Include <IE.au3>
#Include <GUIConstants.au3>
#Include <INet.au3>
$oIE = _IECreate("http://babelfish.altavista.com/tr", 0, 0)
$GUI = GUICreate("XTranzlaterz", 500, 500)
$Edit1 = GUICtrlCreateEdit("", 0, 0, 250, 250)
$Edit2 = GUICtrlCreateEdit("", 0, 250, 250, 250)
$But = GUICtrlCreateButton("Tranzlate", 400, 250)
GUISetState()
While 1
    $Stuff = GUICtrlRead($Edit1)
    $oForums = _IEFormGetObjByName($oIE, "FrmTrText")
    $oEdit = _IEFormElementGetObjByName($oForums, "TrText")
    $oOption = _IEFormElementGetCollection($oForums)
    If GUIGetMsg() = $But Then
        _IEFormElementSetValue($oEdit, $Stuff)
        _IEFormElementOptionselect($oOption, "English to French")
        _IEFormSubmit($oForums)
        _IELoadWait($oIE)
        $dat = _StrBetween(_INetGetSource(_IEPropertyGet($oIE, "locationurl")), "<div style=padding:10px;>", "</div>")
        GUICtrlSetData($Edit2, $dat)
    EndIf
    
WEnd

Func _StrBetween($sString, $sStart, $sEnd, $vCase = -1, $iSRE = -1)
    If $iSRE = -1 Or $iSRE = Default Then
        If $vCase = -1 Or $vCase = Default Then $vCase = 0
        If $vCase <> -1 And $vCase <> Default Then $vCase = 1
        Local $sHold = '', $sSnSStart = '', $sSnSEnd = ''
        While StringLen($sString) > 0
            $sSnSStart = StringInStr($sString, $sStart, $vCase)
            If Not $sSnSStart Then ExitLoop
            $sString = StringTrimLeft($sString, ($sSnSStart + StringLen($sStart)) - 1)
            $sSnSEnd = StringInStr($sString, $sEnd, $vCase)
            If Not $sSnSEnd Then ExitLoop
            $sHold &= StringLeft($sString, $sSnSEnd - 1) & Chr(1)
            $sString = StringTrimLeft($sString, $sSnSEnd)
        WEnd
        If Not $sHold Then Return SetError(1, 0, 0)
        $sHold = StringSplit(StringTrimRight($sHold, 1), Chr(1))
        Local $aArray[UBound($sHold) - 1]
        For $iCC = 1 To UBound($sHold) - 1
            $aArray[$iCC - 1] = $sHold[$iCC]
        Next
        Return $aArray
    Else
        If $vCase = Default Or $vCase = -1 Then $vCase = '(?i)'
        If $vCase <> Default And $vCase <> -1 Then $vCase = ''
        Local $aArray = StringRegExp($sString, '(?s)' & $vCase & $sStart & '(.*?)' & $sEnd, 3)
        If IsArray($aArray) Then Return $aArray
        Return SetError(1, 0, 0)
    EndIf
EndFunc ;Credit ges to Smoke_N for this function

I tried to do a collection object because the select tag has no name. I have also tried tying it into the $oForums variable, but that gave me the same error. Anyone know why this won't work?

Link to comment
Share on other sites

#include <IE.au3>
$oIE = _IECreate ("http://babelfish.altavista.com/tr")
$oForm = _IEFormGetObjByName ($oIE, "FrmTrText")
$oSelect = _IEFormElementGetObjByName ($oForm, "lp")
_IEFormElementOptionselect ($oSelect, "English to French", 1, "byText")

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Ok, thanks. That works fine, but i need help getting the String between. I switched to uing _Sre_BetweenEX found in the stringBetween topic. Heres my code:

#Include <IE.au3>
#Include <GUIConstants.au3>
#Include <INet.au3>
$oIE = _IECreate("http://babelfish.altavista.com/tr", 0, 0)
$GUI = GUICreate("XTranzlaterz", 500, 500)
$Edit1 = GUICtrlCreateEdit("", 0, 0, 250, 250)
$Edit2 = GUICtrlCreateEdit("", 0, 250, 250, 250)
$But = GUICtrlCreateButton("Tranzlate", 400, 250)
GUISetState()
While 1
    $Stuff = GUICtrlRead($Edit1)
    $oForums = _IEFormGetObjByName($oIE, "FrmTrText")
    $oEdit = _IEFormElementGetObjByName($oForums, "TrText")
    $oOption = _IEFormElementGetCollection($oForums)
    $oSelect = _IEFormElementGetObjByName ($oForums, "lp")
    If GUIGetMsg() = $But Then
        _IEFormElementSetValue($oEdit, $Stuff)
_IEFormElementOptionselect ($oSelect, "English to French", 1, "byText")
        _IEFormSubmit($oForums)
        _IELoadWait($oIE)
$div = _SRE_BetweenEX(_INetGetSource(_IEPropertyGet($oIE, "Locationurl")), "<div style", "</div")
        GUICtrlSetData($Edit2, $div)
    EndIf
    
WEnd


Func _SRE_BetweenEX($s_String, $s_Start, $s_End, $iCase = 'i')
    If $iCase <> 'i' Then $iCase = ''
    $a_Array = StringRegExp ($s_String, '(?' & $iCase & _
            ':' & $s_Start & ')(.*?)(?' & $iCase & _
            ':' & $s_End & ')', 3)
    If @extended & IsArray($a_Array) Then Return $a_Array
    Return SetError(1, 0, 0)
EndFunc ;==>_SRE_BetweenEX

This doesen't put anything in the box, but if i give it an array dimension, i get a "Subscript used with non-array variable"

Link to comment
Share on other sites

#Include <IE.au3>
#Include <GUIConstants.au3>
#Include <INet.au3>
$oIE = _IECreate("http://babelfish.altavista.com/tr", 0, 0)
$GUI = GUICreate("XTranzlaterz", 500, 500)
$Edit1 = GUICtrlCreateEdit("", 0, 0, 250, 250)
$Edit2 = GUICtrlCreateEdit("", 0, 250, 250, 250)
$But = GUICtrlCreateButton("Tranzlate", 400, 250)
GUISetState()
While 1
    $Stuff = GUICtrlRead($Edit1)
    $oForums = _IEFormGetObjByName($oIE, "FrmTrText")
    $oEdit = _IEFormElementGetObjByName($oForums, "TrText")
    $oOption = _IEFormElementGetCollection($oForums)
    $oSelect = _IEFormElementGetObjByName ($oForums, "lp")
    If GUIGetMsg() = $But Then
        _IEFormElementSetValue($oEdit, $Stuff)
            _IEFormElementOptionselect ($oSelect, "English to French", 1, "byText")
        _IEFormSubmit($oForums)
        _IELoadWait($oIE)
          $oFromTop = _IEFormGetCollection($oIE, 0)
          $oQuery = _IEFormElementGetCollection ($oFromTop, 0)
        GUICtrlSetData($Edit2, _IEFormElementGetValue ($oQuery))
    EndIf
   
WEnd

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

And GaFrost has saved me! Thanks for the help

Just a another newb when it comes to the ie stuff.

Edit: I was talking about me btw.

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Ok, now the box just returns 0 when there is more than five words in the things to translate. Here's the code:

#Include <IE.au3>
#Include <GUIConstants.au3>
#Include <INet.au3>
$oIE = _IECreate("http://babelfish.altavista.com/tr", 0, 0)
$GUI = GUICreate("XTranzlaterz", 500, 500)
$Edit1 = GUICtrlCreateEdit("", 0, 0, 250, 250)
$Edit2 = GUICtrlCreateEdit("", 0, 250, 250, 250)
$But = GUICtrlCreateButton("Tranzlate", 400, 250)
$Combo = GUICtrlCreateCombo("", 270, 290, 200)
GUICtrlSetData(-1, "English to French|English to German|English to Dutch|English to Italian|Dutch to English|Dutch to French|French to English|French to German")
GUISetState()
While 1
    $Stuff = GUICtrlRead($Edit1)
    $oForums = _IEFormGetObjByName($oIE, "FrmTrText")
    $oEdit = _IEFormElementGetObjByName($oForums, "TrText")
    $oOption = _IEFormElementGetCollection($oForums)
    $oSelect = _IEFormElementGetObjByName ($oForums, "lp")
    If GUIGetMsg() = $But Then
        _IEFormElementSetValue($oEdit, $Stuff)
            _IEFormElementOptionselect ($oSelect, GUICtrlRead($Combo), 1, "byText")
        _IEFormSubmit($oForums)
        _IELoadWait($oIE)
          $oFromTop = _IEFormGetCollection($oIE, 0)
          $oQuery = _IEFormElementGetCollection ($oFromTop, 0)
        GUICtrlSetData($Edit2, _IEFormElementGetValue ($oQuery))
    EndIf
   
WEnd
Link to comment
Share on other sites

I see what you mean, using Firefox I can use Page Info and get the form names and field names.

if the # is small enough then field name "q" is shown with the result

if the # is larger "q" is no longer there but the page still shows the result, not sure where/how it's stored in this manner.

Maybe DaleHohm could give some insight to this one.

#Include <IE.au3>
#Include <GUIConstants.au3>
#Include <INet.au3>
$oIE = _IECreate("http://babelfish.altavista.com/tr", 0, 0)
$GUI = GUICreate("XTranzlaterz", 500, 500)
$Edit1 = GUICtrlCreateEdit("", 0, 0, 250, 250)
$Edit2 = GUICtrlCreateEdit("", 0, 250, 250, 250)
$But = GUICtrlCreateButton("Tranzlate", 400, 250)
$Combo = GUICtrlCreateCombo("", 270, 290, 200)
GUICtrlSetData(-1, "English to French|English to German|English to Dutch|English to Italian|Dutch to English|Dutch to French|French to English|French to German")
GUISetState()
While 1
    $Stuff = GUICtrlRead($Edit1)
    $oForums = _IEFormGetObjByName($oIE, "FrmTrText")
    $oEdit = _IEFormElementGetObjByName($oForums, "TrText")
    $oOption = _IEFormElementGetCollection($oForums)
    $oSelect = _IEFormElementGetObjByName($oForums, "lp")
    If GUIGetMsg() = $But Then
        _IEFormElementSetValue($oEdit, $Stuff)
        _IEFormElementOptionselect($oSelect, GUICtrlRead($Combo), 1, "byText")
        _IEFormSubmit($oForums)
        _IELoadWait($oIE)
        $oFromTop = _IEFormGetCollection($oIE, 0)
        $oQuery = _IEFormElementGetCollection($oFromTop, 0)
        $oQuery = _IEFormElementGetObjByName($oFromTop, "q")
        GUICtrlSetData($Edit2, _IEFormElementGetValue($oQuery))
    EndIf

WEnd

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

As long as the style doesn't change this is the only way I've been able to get it to work

#Include <IE.au3>
#Include <GUIConstants.au3>
$s_Before = '<DIV style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; PADDING-TOP: 10px">'
$s_After = '</DIV></TD></TR>'
$oIE = _IECreate("http://babelfish.altavista.com/tr", 0, 0)
$GUI = GUICreate("XTranzlaterz", 500, 500)
$Edit1 = GUICtrlCreateEdit("", 0, 0, 250, 250)
$Edit2 = GUICtrlCreateEdit("", 0, 250, 250, 250)
$But = GUICtrlCreateButton("Tranzlate", 400, 250)
$Combo = GUICtrlCreateCombo("", 270, 290, 200)
GUICtrlSetData(-1, "English to French|English to German|English to Dutch|English to Italian|Dutch to English|Dutch to French|French to English|French to German")
GUISetState()
While 1
    $Stuff = GUICtrlRead($Edit1)
    $oForums = _IEFormGetObjByName($oIE, "FrmTrText")
    $oEdit = _IEFormElementGetObjByName($oForums, "TrText")
    $oOption = _IEFormElementGetCollection($oForums)
    $oSelect = _IEFormElementGetObjByName ($oForums, "lp")
    If GUIGetMsg() = $But Then
        _IEFormElementSetValue($oEdit, $Stuff)
            _IEFormElementOptionselect ($oSelect, GUICtrlRead($Combo), 1, "byText")
        _IEFormSubmit($oForums)
        _IELoadWait($oIE)
            $sHTML = _IEDocReadHTML($oIE)
            $div = _GetTransLation($sHTML, $s_Before, $s_After)
        GUICtrlSetData($Edit2, $div)
    EndIf
   
WEnd

Func _GetTransLation($sHTML, $before, $after)
    $tmp = StringMid($sHTML, StringInStr($sHTML, $before) + StringLen($before))
    Return StringMid($tmp, 1, StringInStr($tmp, $after) - 1)
EndFunc   ;==>_GetTransLation

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Here ya go...

The data you want is stored in a table cell. The hidden form field you were using does not consistently contain what you want.

I modified the last 3 lines before the Wend EndIf

Dale

#Include <IE.au3>
#Include <GUIConstants.au3>
#Include <INet.au3>

Opt("OnExitFunc", "endscript")

$oIE = _IECreate("http://babelfish.altavista.com/tr", 0, 0)
$GUI = GUICreate("XTranzlaterz", 500, 500)
$Edit1 = GUICtrlCreateEdit("", 0, 0, 250, 250)
$Edit2 = GUICtrlCreateEdit("", 0, 250, 250, 250)
$But = GUICtrlCreateButton("Tranzlate", 400, 250)
$Combo = GUICtrlCreateCombo("", 270, 290, 200)
GUICtrlSetData(-1, "English to French|English to German|English to Dutch|English to Italian|Dutch to English|Dutch to French|French to English|French to German")
GUISetState()
While 1
    $Stuff = GUICtrlRead($Edit1)
    $oForums = _IEFormGetObjByName($oIE, "FrmTrText")
    $oEdit = _IEFormElementGetObjByName($oForums, "TrText")
    $oOption = _IEFormElementGetCollection($oForums)
    $oSelect = _IEFormElementGetObjByName ($oForums, "lp")
    If GUIGetMsg() = $But Then
        _IEFormElementSetValue($oEdit, $Stuff)
            _IEFormElementOptionselect ($oSelect, GUICtrlRead($Combo), 1, "byText")
        _IEFormSubmit($oForums)
        _IELoadWait($oIE)
          ; get a reference to the 6th table (be careful, one of them is commented out in the HTML source)
          $oTable = _IETableGetCollection($oIE, 5) 
          $aTableData = _IETableWriteToArray($oTable)
        GUICtrlSetData($Edit2, $aTableData[0][1])
    EndIf
WEnd

Func endscript()
    _IEQuit($oIE)
EndFunc

Edit: Typo. Also added an _IEQuit in onExitFunc to prevent having an invisible zobmie iexplore.exe

Edited by DaleHohm

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

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