Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you get window ID for xdotool automatically

I am trying to automate testing forms that selenium would take too long (javascript heavy modern forms), and I want to use xdotool and get window IDs. I see you can call xdotool selectwindow and click it, but then you have to click it each time. I want to tell it "for google chrome windows where the tab title is x, do y"

I got the window ID here:

cchilders@cchilders-Dell-Precision-M3800:~$ xdotool selectwindow
65011713

This is for chrome itself, each tab gets the same value when clicked. So I expected to find that in ps or a window manager, but no:

cchilders@cchilders-Dell-Precision-M3800:~$ wmctrl -l
0x03a00001  0 cchilders-Dell-Precision-M3800 views.py - /home/cchilders/work_projects - Atom
0x03a00048  0 cchilders-Dell-Precision-M3800 pip_freeze_update.py - /home/cchilders/scripts - Atom
0x03a000bc  0 cchilders-Dell-Precision-M3800 urls.py - /home/cchilders/work_projects - Atom

nor does ps work:

(clientsite)cchilders@cchilders-Dell-Precision-M3800:~$ ps -alx
F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0     1     0  20   0 185188  5752 ep_pol Ss   ?          0:06 /sbin/init splash
1     0     2     0  20   0      0     0 kthrea S    ?          0:00 [kthreadd]
1     0     3     2  20   0      0     0 smpboo S    ?          0:02 [ksoftirqd/0]
1     0     5     2   0 -20      0     0 worker S<   ?          0:00 [kworker/0:0H]
1     0     7     2  20   0      0     0 rcu_gp S    ?          1:10 [rcu_sched]
1     0     8     2  20   0      0     0 rcu_gp S    ?          0:00 [rcu_bh]
...etc...

nowhere does 65011713 show up. Xdotool is a great tool, but the window manipulation expects you to know a lot about the windows, and from what I remember of using it before, the WINDOW COMMANDS section of https://www.semicomplete.com/projects/xdotool/xdotool.xhtml#window_commands has a lot of ways to find a window you know a lot about, but not much in the way of automating getting that window info. How can I determine the window ID (the format xdotool wants) automatically, say by feeding a script the beginning portion of a URL? Thank you

You can look for Google Chrome in the wmtrl:

(scripts)cchilders@cchilders-Dell-Precision-M3800:~/scripts/bash$ wmctrl -l
0x03e00001  0 cchilders-Dell-Precision-M3800 Edit - Stack Overflow - Google Chrome
...

and grab the first number separated by space to int:

In [13]: int("0x03e00001", 16)
Out[13]: 65011713

The 16 flag in int tells it expect hexadecimal

In [14]: int("0x03e00001")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-96517b980767> in <module>()
----> 1 int("0x03e00001")

ValueError: invalid literal for int() with base 10: '0x03e00001'
like image 345
codyc4321 Avatar asked Dec 10 '15 17:12

codyc4321


1 Answers

You can find a window with xdotool by name, window class, etc. To search for a window by name with, use:

xdotool search --name 'My Window Name'

This will print the decimal window id to stdout. The --name flag matches the part or all of the window name. In a browser, that generally includes the current tab name. xdotool can also return the corresponding pid like this:

xdotool search --name 'My Window Title' getwindowpid

He is an example of sending a series of keypresses and mouseclicks to a window.

# Find window with title containing 'My Window Title, activate it,
# move the mouse to coordinates 200x400, left click, then press F5
xdotool search --name 'My Window Title' windowactivate mousemove 200 400 click 1 key F5
# Store window id of the active window
WINDOW_ID=$(xdotool getactivewindow)
# Type a series of characters into the window
xdotool type "this text is being typed into window" --window $A
like image 113
BenC Avatar answered Sep 21 '22 17:09

BenC