问题
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'
回答1:
You can use awk
to extract the ID from the output of wmctrl -l
.
For example:
wmctrl -l | awk '/Google Chrome/ {print $1}'
xdotool
will likely take that hex IDs just fine but if it can't you can convert that to the decimal representation with strtonum
:
wmctrl -l | awk '/Google Chrome/ {print strtonum($1)}'
How you match just the window you want from the output in awk
is up to you and your requirements.
It is probably worth noting that xdotool
also appears to have a search
command which takes all sorts of specifiers and patterns that you can use to get the window ID of windows you want to operate on. (It even supports a stack of matches that it supports a special format of "window ID" to operate on directly for "chained commands".)
来源:https://stackoverflow.com/questions/34207981/how-do-you-get-window-id-for-xdotool-automatically