Search code examples
pythonautomationbotstorpywinauto

Can I reconnect to the main window with PyWinAuto?


I reach open browser, click in the button on the screen and click in the button of the pop up window without problems. The only problem is, when I close pop up window clicking the button "Close Tor Browser", I can't reconnect with my previous window (the main and first window). Any tips??

from pywinauto.application import Application
import time

for i in range(0,2):
    try:
        for i in range(0,1):
            try:
                app=Application(backend='uia').start('\...\firefox.exe')
                app=Application(backend='uia').connect(title='Connect to Tor — Tor Browser',timeout=40)
                time.sleep(5)
                app.window(best_match='Dialog', top_level_only=True).child_window(best_match='Ver todos los servicios').click()

            except Exception:
                time.sleep(1)
                app2=Application(backend='uia').connect(title='Close Tor Browser',timeout=40)
                app2.window(best_match='Dialog', top_level_only=True).child_window(best_match='Cancel').click()
                
        time.sleep(5)
        app=Application(backend='uia').connect(title='Connect to Tor — Tor Browser',timeout=40)   
        top_level_only=True).child_window(best_match='Ver todos los servicios').click()
        time.sleep(10)
        app.kill()
        time.sleep(1)

    except Exception:
        pass

I can open browser easily, click in the button on the screen and click in the button of the pop up window without problems. The only problem is, when I close pop up window clicking the button "Close Tor Browser", I can't reconnect with my previous window (the main and first window). Any tips??


Solution

  • Best bet to contorl (assuming extension has interactable popout [e.g. options, whatever, once you click/depress specfiiced hotkey) as follows (kukos to BrowserStack folk, and RobWuRobWu - CRX / page taxomy etc.).

    This is the soluiton I explore here - will leave link in comment for altenrative concerning pywinauto method that should also achieve what you need (uses elements of objects instead of inaccurate pic identification, although it can do that too I believe, the backend method may be able to serve your case RE: using a diferent deskopt at the same time).

    Recommneded approach:


    1. Get ID for extension (in URL on respective Webstore page, if one exists, or in app details within settings)

    [Settings: she's highlighted in blue :))1

    OR

    Chrome webstore - there's it is again

    i.e. https://chrome.yada.yada.yada.then.BOOM./dknlfmjaanfblgfdfebhijalfmhmjjjo


    1. Plug this ID into the following url in place of YOURID (take out the latter, put the former in its place):

    Yes it has two https: don't get hung up by that its supposed to. See references / blog site below if you feel sceptical RE: legitamicy/validity etc. ¯_(ツ)_/¯

    This will provide a webpage that looks like this - with useful detail regarding the extension - each file/pop-up html component (LHS), wiht RegEx to find the page you want to automate.

    RobWu site

    • Notice you can download crx and unloaded package from here - but that's or another rainy day - back to the Story Rory
    • In this example I get a popup out when I clikc on the extension - so I've filtered for all htmls, per penultimate pic below, to retieve ultimate pic for this step:

    RegEx filter - all html

    Phew! Left with just one html - which sounds very promising given it's entitled 'popup.html' (this is non uncommon) -


    1. You now have popup.html (let's call this the OPTION_PAGE - use this in the following url (again, substituting where appropriate your extension's details in the obvious plaecs in this url and voila! (first belo = generaic e.g., min is live demo! :)

    2. chrome-extension://**YOUR_ID**/**YOUR_OPTION_PAGE** which became (in my case)

    3. chrome-extension://dknlfmjaanfblgfdfebhijalfmhmjjjo/popup.html

    And it opens to a full webpage comprising the relevant interactabel / programmaticly friendly option page !

    ( ᵔ ͜ʖ ͡ᵔ)


    **Notes/sample example:

    Feaures:

    1. Can be more up to date/looking ward
    2. No requirement to contorlhistoric with physical intevention
    3. Should be able to run in hidden mode (that woold be great - but prob. not allowed! :)
    4. Ability to multi-procewss & especially demand modelling

    Code to sign into noptcha using a dummy ID (go ahead give it a go - you might get lucky! not :))

    Some context RE: variables/fns:

    p element of vector ps =[0,1,2,...,n] for n browsers - you can ignore this and pretend variables like d[p] simply represented driver, where driver = webdriver.Chrome(options=o, executable_path=path_exec)

    activ(p) activates the pth browser - ignore

    ss = time.time (so ss(0.5) aka time.time(0.5) = 0.5 sec delay)

    import time
    
    from selenium.webdriver.common.action_chains import ActionChains as AC
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options
    from concurrent.futures import hreadPoolExecutor, ProcessPoolExecutor  
    #*(see [here][7]) - as I say, not essential* 
    
    
    
    def nopecha(p):
    
        print('nopecha')
        global d # selenium webdriver - with early thinking as wemight have given to access, and done the hand me donw ting L)
        #d[p].implicitly_wait(10)
        activ(p) #this activates window in quesiton
        d[p].get("chrome-extension://dknlfmjaanfblgfdfebhijalfmhmjjjo/popup.html")
        d[p].find_element_by_class_name('plan_button.clickable').click()
        ss(0.5)
        AC(d[p]).key_down(Keys.CONTROL).send_keys('a').key_up('keys.CONTROL').perform() #send_keys_to_element()
        #kd('ctrl'), pr('a'), ku('ctrl'), pr('delete')
        ss(0.5)
        el = d[p].find_element_by_class_name('plan_info')
        # d[p].find_element_by_class_name('plan_info').clear()
        AC(d[p]).send_keys_to_element(el, 'made-up-example-for-extension-login-serial-hope-you-understand-ta-k-bye-now').perform() #note this doesn't hijack your mouse/keyboard - you could still be using it while multiple browsers being threaded - as in this case 
        ss(0.5)
        AC(d[p]).send_keys_to_element(el, Keys.ENTER).perform()
        # d[p].find_element_by_class_name('plan_button.clickable').click()
        # AC(d[p]).send_keys(Keys.ENTER).perform()
        try:
            d[p].find_element_by_class_name('btn off').click()
        except:
            pass
        el = ev[p]('''return document.querySelector('.menu').children[1].lastElementChild''')
        el.click(), cc(2)
        with UIPath(u"chrome-extension://dknlfmjaanfblgfdfebhijalfmhmjjjo/popup.html - Chromium||Pane"):
            click(u"||Document->Image||Text")
            click(u"||Document->Speech||Text")
    
        # for p in ps: th_0(p)
    

    Gif shot: