Pywinauto: find_elements found nothing (remotely from Ansible, using WinRM)

Created on 21 Aug 2017  路  18Comments  路  Source: pywinauto/pywinauto

Hi,
The above code runs well in local environment,it will return the elements.But when it be called from remote(ansible),it would return nothing.So the 46 line in taksbar.py will throw "indexerror....".
The attachments have the code detail and error info.Would you pls help? Thanks !
environment:pywinauto 0.6+, python 2.7.13
os: 2008 r2 sp1

ran well in local:
run_well_in_local

found nothing when ran by ansible:
remote_run_found_nothing

documentation question

Most helpful comment

@vasily-v-ryabov No problem.Thank you for giving me the directives which saves me lots of time
1, test1.yml

---
- name: test ra module
  hosts: *****
  tasks:
    - name: run auto
      win_psexec: 
        command: python pywinauto_example.py
        executable: C:\Windows\PSTools\psexec.exe
        interactive: yes
        username: admin
        password: ******
        hostnames: ******

2,run : ansible-playbook test1.yml

And I found that new a schedules tasks works too,in ansible just one command(the rapwd was the task I created in the remote windows):
ansible * -m raw -a 'schtasks /run /tn rapwd'

All 18 comments

I'm afraid Ansible runs the script through SSH.
GUI is not accessible by SSH (protocol restriction).

It can be done using Jenkins slave on Windows test machine. But Jenkins slave must run in the active GUI session. If you use RDP, you can't use methods .click_input(), .type_keys(...) and derived ones. Or you have to keep RDP always open and not minimized (can be in non-full-screen mode so that you can switch to another window on local machine). But it's better to setup VNC server on test machine (I used TightVNC at my previous job), so disconnected VNC will keep an active GUI context because it's not a virtual desktop. If you see black screen in VNC client on local machine, you might need to install video drivers on remote test machine where VNC server is installed.

It's pretty hard for the first time. I'm thinking to write a brief tutorial with the screenshots.

If Jenkins slave runs as a service, it will not be able to access GUI as well (OS Windows restriction).

Sorry, I see it's WinRM, not SSH.
Need to investigate if it's possible through WinRM or not.

@vasily-v-ryabov Yes, it's through WinRM.Thanks

I see Windows Remore Management (WS-Management) is running as a service under "Network Services" account. It has no UI access obviously.

More deep investigation may help to make it possible. The link to discussion:
https://serverfault.com/questions/690852/use-powershell-to-start-a-gui-program-on-a-remote-machine
But not sure it's easy to port into Ansible.

@vasily-v-ryabov Thanks a lot .Yes ,as the link you post,I had tried to use the psexec to do this(Ansible has the win_psexec module),and it works.Maybe it's simplest way.

@yangliang003 that's great! Can you post an example of ansible command for that? I will add it to the new docs page later.

@vasily-v-ryabov No problem.Thank you for giving me the directives which saves me lots of time
1, test1.yml

---
- name: test ra module
  hosts: *****
  tasks:
    - name: run auto
      win_psexec: 
        command: python pywinauto_example.py
        executable: C:\Windows\PSTools\psexec.exe
        interactive: yes
        username: admin
        password: ******
        hostnames: ******

2,run : ansible-playbook test1.yml

And I found that new a schedules tasks works too,in ansible just one command(the rapwd was the task I created in the remote windows):
ansible * -m raw -a 'schtasks /run /tn rapwd'

Thanks! You mean rapwd is a name of the task in Windows Task Scheduler (which is available in Computer Management app: System Tools -> Task Scheduler -> Task Scheduler Library), am I right?

@vasily-v-ryabov Yes,you are right(The task never runs itself,because I set it's run time as an passed time,so it only runs by ansible call).
The command to create scheduled task "rapwd":
Schtasks /Create /tn rapwd /tr c:\Python27\rapwd.bat /sc ONCE /st hh:mi:ss /sd yyyy/mm/dd
and the "rapwd.bat" just runs the pywinauto code.

@yangliang003 thank you!
And what about methods .click_input() and .type_keys(...)? Are they working with this approach?

@vasily-v-ryabov yes, both work well

Remote Execution Guide draft is here: #589.

The guide seems finished for now. Closing the issue.
https://pywinauto.readthedocs.io/en/latest/remote_execution.html

There is other way that you can control gui application on remote Desktop with pywinauto. You can use rpyc or Remote Python Call. that is a python library for rpc(remote procedure call)

https://rpyc.readthedocs.io/en/latest/

your remote computer is as server and your host is as client in rpyc.

first you you need to run rpyc_classic.py in remote computer after connect to it and use pywinauto library. for example i use rpyc for start microsip on remote computer with pywinauto.

ip of remote computer is 192.168.222.222.

import rpyc
ip = "192.168.222.222"
conn = rpyc.classic.connect(ip)
conn.execute("from pywinauto import Application")
conn.execute(r"Application().start(r'C:\Program Files (x86)\MicroSIP\microsip.exe')")

@greenmns thanks! This is interesting, but it doesn't differ so much from copying Python script and starting it on a remote PC. The only advantage of rpyc is a dynamic and Pythonic interaction between local and remote hosts. We can think about adding this way to the Remote Execution Guide. Also a pull request is welcome.

Some users tell me about their requirement to keep remote host clean (no Python installed, copying files to remote host is also prohibited). But I think such restrictions can't be circumvented.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lanzorg picture lanzorg  路  16Comments

rajarameshmamidi picture rajarameshmamidi  路  38Comments

rajarameshmamidi picture rajarameshmamidi  路  14Comments

jjbright picture jjbright  路  15Comments

MagazinnikIvan picture MagazinnikIvan  路  14Comments