tebelorg / RPA-Python

Python package for doing RPA
Apache License 2.0
4.94k stars 667 forks source link

Visual automation not working when calling script with subprocess - some ideas #260

Closed iCreamble closed 3 years ago

iCreamble commented 3 years ago

Hey guys. Im trying to create a py script that calls other py scripts when they are needed. Each different script will have a different automation purpose. It happens that, when I call a script that has visual automation purposes (with subprocess call command), the visual automation does not work, not recognizing the elements.

Command py script example:

import win32com.client as win32
import ctypes
import subprocess

class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

with disable_file_system_redirection():
    subprocess.call(r'script.py', shell=True)

Automation script example:

import rpa as r
import win32com.client as win32
import ctypes
import subprocess

def ispresentlooptype(element, text):
    while 1==1:
        hue = r.present(element)
        print(hue)
        if hue == True:
            break
    r.type(element, text)

def ispresentloopclick(element):
    while 1==1:
        hue = r.present(element)
        print(hue)
        if hue == True:
            break
    r.click(element)

class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

r.init(visual_automation = True, chrome_browser=False)
r.vision('Settings.MoveMouseDelay = 0')

with disable_file_system_redirection():
    subprocess.call(r'explorer.exe "C:\Program Files\Internet Explorer\iexplore.exe"')

ispresentlooptype('url_ie.PNG', 'website')
ispresentlooptype('login_oracle.PNG', 'user')
r.type('senha_oracle.PNG', 'pwd')
r.click('botao_login.PNG')
ispresentloopclick('fio_inv.PNG')
r.click('transacoes.PNG')
r.click('transf_subinv.PNG')
r.click('java_update_no.PNG')
ispresentloopclick('java_accept.PNG')
r.click('java_accept_button.PNG')
r.click('cancelar_org.PNG')
r.close()
kensoh commented 3 years ago

Hi @iCreamble, are you saying that running python script.py everything works ok but running subprocess.call(r'script.py', shell=True) fails? Here are some ideas to explore -

  1. try running with import os; os.system('python script.py') to see what happens

  2. I'll think using subprocess.call() the input would be with python script.py instead or just script.py

  3. for running a python script within another, normally you can use exec(open('script.py').read()) (for Python 3)

  4. If above fails, can you tell me more about what is the error? and give attach the log file here? You can get the log file location from print(r.tagui_location() + '/.tagui/src/tagui.sikuli/tagui.log')

iCreamble commented 3 years ago

Hey, I found the problem. I did not add the absolute path to the picture names, so the automation script was trying to find the picture on the Command script's path. Now that I have added the absolute path, it works like a charm. Thanks!

kensoh commented 3 years ago

Oh great, that make sense! I didn't of that. Let me know if you run into further issues :)