Jump to content

IE Clicking the Hidden Button


Recommended Posts

Hi All,

I am trying to write a script for this site (https://www.onlinedoctranslator.com/translationform), that automatically updates a file and translate it to a destination language. 

However, the "translate" button only shows up after uploading finishes. Is there anyway that can have the script to wait for the uploading to finish? I tried _ieloadwait but doesn't work well.

 

Also, I cannot seem to have the script to click the hidden button after the uploads. Can anyone please help me?

 

Really appreciate all the help!!

TG

Link to comment
Share on other sites

You'll have to loop, looking for your button...then when you find it, check it's attributes to verify it is visible.  If you don't find it, then loop again.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

#include <IE.au3>
Global $DocTranslator = "https://www.onlinedoctranslator.com/translationform"
global $oIE = _IECreate()
_IENavigate($oIE, $DocTranslator)
_IELoadWait($oIE, $DocTranslator)
global $tags = $oIE.document.GetElementsByTagName("div")
global $oInputs = _IETagNameGetCollection($oIE, $tags)
For $oInput In $oInputs
    if $oInput.class == "cta high translation-button" Then
        $oInput.Click
        ExitLoop
Next

_IEQuit($oIE)

This is what I had so far.

Here's the html source code relating to the button:

<input type="submit" id="translation-button" class="cta high translation-button" value="Translate" style="display: inline-block;">

if(canSubmit === false){
    $('#translation-button').prop("disabled", true);
    }
else{
    $('#translation-button').prop("disabled", false);

 

Link to comment
Share on other sites

What is the style when it's not visible, and when it is.  You can use my signature, and adjust the wait time.

$sXML = "//div//input[@class='cta high translation-button' AND Contains(@style,'display:')]"

It will do all the loops and logic for you.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

On 6/21/2017 at 6:41 PM, Danp2 said:

How do you intend to upload the document? Is someone going to manually drag it to the page?

I'm having the AutoIt to click in the upload area, WinWait the pop up window and send the directory of the file.

Link to comment
Share on other sites

6 hours ago, Peaky said:

I'm having the AutoIt to click in the upload area, WinWait the pop up window and send the directory of the file.

Ok... if that piece is already working, I would recommend the following sequence --

  • Set the source and destination languages first
  • Initiate the file upload
  • Wait until the Translate button is enable and then click it

Check out the IEbyXPATH UDF as suggested by @jdelaney and let us know if you have any further questions.

Link to comment
Share on other sites

Ok, just thinking... why would you use IE to use some web service that uses google translator to translate some file? And go thru all this "automation" stuff?

Wouldn't it be easier to use google translator directly, without IE, and without the use of third party web service?

Maybe (but only maybe) like this:

#include "WinHttp.au3"

$sAddress = "https://translate.googleusercontent.com/translate_f"
$sOutputLang = "fr"

$sFileToUpload = FileOpenDialog("Choose file", "", "Text file (*.txt)")

If Not $sFileToUpload Then Exit

$sForm = _
        '<form action="' & $sAddress & '" method="post" enctype="multipart/form-data">' & _
        ' <input type="file" name="file"/>' & _ ;
        ' <input name="sl" />' & _
        ' <input name="tl" />' & _
        ' <input name="js" />' & _
        ' <input name="prev" />' & _
        ' <input name="hl" />' & _
        ' <input name="ie" />' & _
        ' <input name="text" />' & _
        ' <input name="edit-text" />' & _
        '</form>'

; Initialize and get session handle
$hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063")

$hConnect = $sForm ; will pass form as string so this is for coding correctness because $hConnect goes in byref

; Fill form
$sHTML = _WinHttpSimpleFormFill($hConnect, $hOpen, _
        Default, _
        "name:file", $sFileToUpload, _
        "name:sl", "auto", _
        "name:tl", $sOutputLang)
$iErr = @error

; Close handles
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

If $iErr Then
    MsgBox(4096, "Error", "Error number = " & @error)
Else
    Local $aData = StringRegExp($sHTML, "(?si)<\s*pre(?:[^\w])\s*(.*?)(?:(?:<\s*/pre\s*>)|\Z)", 3)
    If @error Then
        MsgBox(4096, "Error", "Check the console output!")
        ConsoleWrite($sHTML & @CRLF)
    Else
        $hOutputFile = FileOpen($sFileToUpload & "_" & $sOutputLang & ".txt", 130)
        FileWrite($hOutputFile, $aData[0])
        FileClose($hOutputFile)
;~      ConsoleWrite($aData[0] & @CRLF)
    EndIf

EndIf

 

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

5 hours ago, trancexx said:

Ok, just thinking... why would you use IE to use some web service that uses google translator to translate some file? And go thru all this "automation" stuff?

Wouldn't it be easier to use google translator directly, without IE, and without the use of third party web service?

Maybe (but only maybe) like this:

#include "WinHttp.au3"

$sAddress = "https://translate.googleusercontent.com/translate_f"
$sOutputLang = "fr"

$sFileToUpload = FileOpenDialog("Choose file", "", "Text file (*.txt)")

If Not $sFileToUpload Then Exit

$sForm = _
        '<form action="' & $sAddress & '" method="post" enctype="multipart/form-data">' & _
        ' <input type="file" name="file"/>' & _ ;
        ' <input name="sl" />' & _
        ' <input name="tl" />' & _
        ' <input name="js" />' & _
        ' <input name="prev" />' & _
        ' <input name="hl" />' & _
        ' <input name="ie" />' & _
        ' <input name="text" />' & _
        ' <input name="edit-text" />' & _
        '</form>'

; Initialize and get session handle
$hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063")

$hConnect = $sForm ; will pass form as string so this is for coding correctness because $hConnect goes in byref

; Fill form
$sHTML = _WinHttpSimpleFormFill($hConnect, $hOpen, _
        Default, _
        "name:file", $sFileToUpload, _
        "name:sl", "auto", _
        "name:tl", $sOutputLang)
$iErr = @error

; Close handles
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

If $iErr Then
    MsgBox(4096, "Error", "Error number = " & @error)
Else
    Local $aData = StringRegExp($sHTML, "(?si)<\s*pre(?:[^\w])\s*(.*?)(?:(?:<\s*/pre\s*>)|\Z)", 3)
    If @error Then
        MsgBox(4096, "Error", "Check the console output!")
        ConsoleWrite($sHTML & @CRLF)
    Else
        $hOutputFile = FileOpen($sFileToUpload & "_" & $sOutputLang & ".txt", 130)
        FileWrite($hOutputFile, $aData[0])
        FileClose($hOutputFile)
;~      ConsoleWrite($aData[0] & @CRLF)
    EndIf

EndIf

Hi trancexx, thank you for all your help. The reason why I need to use that website is because it uses Google Translate API but keeps the format of the file. This way, it saves me a lot of editing time on correcting the layouts.

 

Link to comment
Share on other sites

7 hours ago, Danp2 said:

Ok... if that piece is already working, I would recommend the following sequence --

  • Set the source and destination languages first
  • Initiate the file upload
  • Wait until the Translate button is enable and then click it

Check out the IEbyXPATH UDF as suggested by @jdelaney and let us know if you have any further questions.

That's what I'm trying to do right now :> but since I'm fairly new to scripting, it might take me weeks to even understand his code LOL

Link to comment
Share on other sites

On 6/22/2017 at 10:52 PM, Peaky said:

I'm having the AutoIt to click in the upload area, WinWait the pop up window and send the directory of the file.

Here's a working example sans the upload functionality --

#include <IE.au3>

$DocTranslator = "https://www.onlinedoctranslator.com/translationform"
$oIE = _IECreate($DocTranslator)

$oForm = _IEFormGetObjByName($oIE, "translation-form")
$oSelectFrom = _IEFormElementGetObjByName($oForm, "from")
$oSelectTo = _IEFormElementGetObjByName($oForm, "to")

_IEFormElementOptionSelect($oSelectFrom, "English", 1, "byText")
_IEFormElementOptionSelect($oSelectTo, "Spanish", 1, "byText")

;--
;-- Your code to upload the file goes here
;--

$oButton = _IEGetObjById($oForm, "translation-button")

$i = 0

Do
    Sleep(1000)
    $i += 1
Until $i > 30 Or Not $oButton.Disabled

If Not $oButton.Disabled Then
    _IEAction($oButton, 'click')
EndIf

 

Link to comment
Share on other sites

Thank you all for your help!! I was able to make it work with the following codes, just in case if it's helpful to anyone else:

 

$sURL = "https://www.onlinedoctranslator.com/translationform"           ;working in IE
$oIE = _IECreate($sURL)
$hIE = _IEPropertyGet($oIE, "hwnd")
Global $oForm = _IEFormGetObjByName($oIE, "translation-form")
$oButton = _IETagNameGetCollection($oIE, "Button", 0)

 If @error = 0 And IsObj($oButton) Then _IEAction($oButton, "focus")
 ControlSend($hIE, "", "", "{ENTER}")


While 1
    If WinWait("[CLASS:#32770]", "", 2) Then
        ConsoleWrite("Debug: Detected update prompt window" & @LF)
        $hWin = WinGetHandle("[CLASS:#32770]","")
        WinActivate($hWin)
        Sleep(1000)
       ControlFocus("Choose File to Upload","","Edit1")
        Sleep(1000)
       ControlSetText("Choose File to Upload","","Edit1",_RemoveFileExt($FileList[$i])&".docx")
        Sleep(1000)
       ControlClick("Choose File to Upload","","Button1")
        Sleep(1000)

$Translation = _IEFormGetObjByName($oIE, "translation-form")
$TranslationFrom = _IEFormElementGetObjByName($Translation, "from")
_IEFormElementOptionSelect($TranslationFrom, "Chinese", 1, "byText")
$TranslationTo = _IEFormElementGetObjByName($Translation, "to")
_IEFormElementOptionSelect($TranslationTo, "English", 1, "byText")

  Sleep(30000)

_IEFormSubmit($oForm)

  Sleep(60000)

Local $hIE2 = WinGetHandle("[Class:IEFrame]")
Local $hCtrl = ControlGetHandle($hIE2, "", "[ClassNN:DirectUIHWND1]")

ControlSend($hIE2, "", $hCtrl, "!S")

 Sleep(10000)

        ExitLoop
    Else
        ConsoleWrite("Debug: Update prompt window not detected yet..." & @LF)
    EndIf
WEnd

Sleep(500)


_IEQuit($oIE)
Sleep(5000)

 

Awesome community, really appreciate the help!!

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