sergey_slash Posted April 28, 2020 Posted April 28, 2020 Hi i've been trying messing with autoit, amazing tool. so i wanted to do this automated translator of a phrase into multiple languages. long story short i am experimenting with deepl.com/translator/ ... i am so far just playing around, but tried 2 methods. 1. by imitating clicks - interestingly works like a charm, i can open chrome, open translator, insert text into the textarea, all this by imitating keystrokes with Send(), but of course when it comes to translating into multiple languages, i need to select them via the menu on the right above the translated text textarea.. it is doable by imitating keystrokes or clicks, of course, just need to count them off or go by pixels, or something... but this is all very unstable imho. and so i decided to try webdriver. 2. with webdriver - omg what a nice tool, thanks to everyone for the ability to use it, Danp2 mostly. and so i am able to open the website and obviously now I need to imitate clicks on certain elements of the html. i was able to locate the element and click it - but receiving error local $langButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//button[text()='English']") error WD_Post: StatusCode=400; ResponseText={"value":{"error":"element not interactable","message":"element not interactable\n (Session info: chrome=81.0.4044.122)","stacktrace":" and so i thought maybe it needs to open the menu first, so i figured the xPath and tried this local $langButton1 = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//button[text()='Any language (detect)']") and again i am getting element was found, but not interactable.. what does this mean? do i need to trace what JS is involved with these buttons and try to invoke it? it's not a <select, just a <div with bunch of button tags.. please, help, any advice will be appreciated. so far the only "solution" i see is to open multiple tabs, set all languages there, and then do translations by switching from tab to tab... the whole job is going to be translating short texts into multiple languages, after i figure what to do with deepl, will also involve google translate for those languages that deepl doesn't have.
Moderators Melba23 Posted April 28, 2020 Moderators Posted April 28, 2020 (edited) Moved to the appropriate forum, as the Developer General Discussion forum very clearly states: Quote General development and scripting discussions. Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums. Expand Moderation Team Edited April 28, 2020 by Melba23 Jon changed the forum heading! Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Reveal hidden contents ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
sergey_slash Posted April 28, 2020 Author Posted April 28, 2020 can't find edit option, so will just add here, with webdriver i am basically stuck on switching language of "translate from" part, to set it to English, to make sure it doesn't use auto-detect..
Danp2 Posted April 28, 2020 Posted April 28, 2020 On 4/28/2020 at 4:40 PM, sergey_slash said: so i thought maybe it needs to open the menu first Expand That's correct, because you can't use Webdriver to click an element that isn't visible. It seems like you are targeting the wrong element on your latest attempt. I would try this -- $xpath = "//div[@class='lmt__language_select lmt__language_select--source']//button[@class='lmt__language_select__active']" ; From Chropath $langButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $xpath) _WD_ElementAction($sSession, $langButton, 'click') Now the menu should be visible so that you can click the "English" element. sergey_slash 1 Latest Webdriver UDF Release Webdriver Wiki FAQs
sergey_slash Posted April 28, 2020 Author Posted April 28, 2020 @Danp2 yes, i see it now, thanks! my English button click now works. interestingly, it selects the language, but doesn't close the language selection menu... thanks again for the tools!
sergey_slash Posted April 29, 2020 Author Posted April 29, 2020 since we are talking and looking, i just have this curious question... when i pick the language to translate to, select the button and click it, - it for some reason shows the previous language in the list.. obviously it's not a problem, but with this I cannot use the last language that they offer.. this must be something quirky in their JS? $xpath = "//div[@class='lmt__language_select lmt__language_select--target lmt__language_select--open lmt__language_select--open_2']//button[contains(text(),'Russian')]" Local $langButtonTo = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $xpath) _WD_ElementAction($sSession, $langButtonTo, 'click') when I run this code, the selected language is Polish (the one before Russian)...
Danp2 Posted April 29, 2020 Posted April 29, 2020 Your xpath doesn't work for me, so it's difficult to know how to advise you. Can you post a short but functional script that we can run to observe the issue? Latest Webdriver UDF Release Webdriver Wiki FAQs
sergey_slash Posted April 29, 2020 Author Posted April 29, 2020 oh, it's the 2nd step, first gotta click the dropdown, then the link. as you said, it cannot click invisible link. here's my code so far expandcollapse popupConst $newChromeTabTitle = "New Tab - Google Chrome", $trDeepL = "https://www.deepl.com/translator", $sDeepL = "deepl.com" Local $sDesiredCapabilities, $sSession _WD_Option('Driver', 'chromedriver.exe') _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true }}}}' _WD_Startup() ;start web driver $sSession = _WD_CreateSession($sDesiredCapabilities) _WD_Navigate($sSession, $trDeepL) ;wait for input textarea to be there.. Local $txtArea = "//textarea[@class='lmt__textarea lmt__source_textarea lmt__textarea_base_style']" Local $txtAreaTo = "//textarea[@class='lmt__textarea lmt__target_textarea lmt__textarea_base_style']" _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, $txtArea) ;set text Local $inputTextArea = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $txtArea) _WD_ElementAction($sSession, $inputTextArea, 'value', "translate this sentence") ;now look up the menu drop down for input language, set english Local $xpath = "//div[@class='lmt__language_select lmt__language_select--source']//button[@class='lmt__language_select__active']" Local $langButtonMenu = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $xpath) If @error <> $_WD_ERROR_Success Then _WD_ElementAction($sSession, $langButtonMenu, 'click') EndIf ;and in the language menu select source language EN $xpath = "//div[@class='lmt__language_select lmt__language_select--source lmt__language_select--open lmt__language_select--open_2']//button[contains(text(),'English')]" ; Local $langButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $xpath) _WD_ElementAction($sSession, $langButton, 'click') ;same for target language $xpath = "//div[@class='lmt__language_select lmt__language_select--target']//button[@class='lmt__language_select__active']" Local $langButtonToMenu = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $xpath) _WD_ElementAction($sSession, $langButtonToMenu, 'click') ;and in the language menu select target language RU $xpath = "//div[@class='lmt__language_select lmt__language_select--target lmt__language_select--open lmt__language_select--open_2']//button[contains(text(),'Russian')]" Local $langButtonTo = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $xpath) _WD_ElementAction($sSession, $langButtonTo, 'click') _WD_LoadWait($sSession, 3000) Local $targetTextArea = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $txtAreaTo) $translatedText = _WD_ElementAction($sSession, $targetTextArea, 'property', 'value') MsgBox(0,"info",@CRLF & $translatedText & @CRLF) _WD_DeleteSession($sSession) _WD_Shutdown() xpaths are really identical on both sides, just difference in source and target parts, and source side selects proper language while target part selects previous language.. since i am doing multi-language translator, i figured the less I access deepl and google websites - the better, to avoid possible too many requests throttling (tried something from github got 429 too many requests before even was able to test it).. so I am planning to open the site 1 time and go thru the menu, select every option, get translation, save somewhere, etc...
sergey_slash Posted April 29, 2020 Author Posted April 29, 2020 ok i got it ... apparently it clicks the invisible element, so i don't really need to click the menu.. for some reason if I click the menu first - it selects previous language, but if i skip the Menu click, as it's done with the left side thanks to the IF THEN there, then it properly selects the right language... just weird... solved, i guess.
Danp2 Posted April 29, 2020 Posted April 29, 2020 @sergey_slash There were a few issues with your script -- You were clicking on failure instead of success Need to wait until element is visible before clicking Here's a modified version that works for me -- expandcollapse popup#include "wd_core.au3" #include "wd_helper.au3" Const $newChromeTabTitle = "New Tab - Google Chrome", $trDeepL = "https://www.deepl.com/translator", $sDeepL = "deepl.com" Local $sDesiredCapabilities, $sSession _WD_Option('Driver', 'chromedriver.exe') _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true }}}}' _WD_Startup() ;start web driver $sSession = _WD_CreateSession($sDesiredCapabilities) _WD_Navigate($sSession, $trDeepL) ;wait for input textarea to be there.. Local $txtArea = "//textarea[@class='lmt__textarea lmt__source_textarea lmt__textarea_base_style']" Local $txtAreaTo = "//textarea[@class='lmt__textarea lmt__target_textarea lmt__textarea_base_style']" _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, $txtArea) ;now look up the menu drop down for input language, set english Local $xpath = "//div[@class='lmt__language_select lmt__language_select--source']//button[@class='lmt__language_select__active']" Local $langButtonMenu = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $xpath) If @error = $_WD_ERROR_Success Then _WD_ElementAction($sSession, $langButtonMenu, 'click') EndIf ;and in the language menu select source language EN $xpath = "//div[@class='lmt__language_select lmt__language_select--source lmt__language_select--open lmt__language_select--open_2']//button[contains(text(),'English')]" _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, $xpath, Default, Default, True) Local $langButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $xpath) If @error = $_WD_ERROR_Success Then _WD_ElementAction($sSession, $langButton, 'click') EndIf ;same for target language $xpath = "//div[@class='lmt__language_select lmt__language_select--target']//button[@class='lmt__language_select__active']" Local $langButtonToMenu = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $xpath) _WD_ElementAction($sSession, $langButtonToMenu, 'click') ;and in the language menu select target language RU $xpath = "//div[@class='lmt__language_select lmt__language_select--target lmt__language_select--open lmt__language_select--open_2']//button[contains(text(),'Russian')]" _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, $xpath, Default, Default, True) Local $langButtonTo = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $xpath) If @error = $_WD_ERROR_Success Then _WD_ElementAction($sSession, $langButtonTo, 'click') EndIf ;set text Local $inputTextArea = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $txtArea) _WD_ElementAction($sSession, $inputTextArea, 'value', "translate this sentence") _WD_LoadWait($sSession, 3000) Local $targetTextArea = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $txtAreaTo) $translatedText = _WD_ElementAction($sSession, $targetTextArea, 'property', 'value') MsgBox(0,"info",@CRLF & $translatedText & @CRLF) _WD_DeleteSession($sSession) _WD_Shutdown() Latest Webdriver UDF Release Webdriver Wiki FAQs
sergey_slash Posted May 2, 2020 Author Posted May 2, 2020 thanks for pointing out that i needed to wait for the element to be visible. my input text is formatted well enough to automate translation, so far I've been reading the file, extracting english text, there's array in 1 place, so a lot of situations to take care of. but I do have working DeepL part for all languages it has, now just need google part.. And then next 2 months I can just observe work happen or just watch TV... it is awesome 😃
CYCho Posted May 2, 2020 Posted May 2, 2020 On 5/2/2020 at 2:58 AM, sergey_slash said: now just need google part Expand You can take a look at my earlier post on google translate. zPlayer - A Small Audio and Video Player
sergey_slash Posted May 2, 2020 Author Posted May 2, 2020 thanks, but I will be translating big amounts of text, so call by URL doesn't work for me, it will soon be getting "429 too many requests errors". my method opens 2 browser tabs and works with them by switching languages and copying the text. plus i am using WebDriver, it's a completely different approach.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now