how do you get window ID for xdotool automatically
Asked Answered
G

4

10

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'
Galactose answered 10/12, 2015 at 17:20 Comment(8)
65011713 is a binary value. The values returned from wmctrl are hex values. You'll need to convert to have them match (though none of those three do but then I don't see chrome in that list at all). Nothing in ps is at all relevant here though. If you can't get individual window IDs for the tabs though (and you may not be able to) then you likely can't target a tab specifically (or necessarily even see the tabs with xdotool in the first place). (It might be worth looking at the output from xwininfo -tree to see if you can find what you are looking for there.)Pitta
yes, that's correct, it's a hassle but inting it solves itGalactose
phrase yours as a question if you want me pick it. and I almost lol'ed my spleen inside out at dude voting to close something this specific. <3 SOGalactose
Did you just update your question with the answer you worked out for yourself?Pitta
ya that's a summary, but it's your answer. if you add an answer I'll pick itGalactose
With selenium you can switch between tabs. #12729765Wrath
awesome, thanks funivanGalactose
Nowadays (xdotool version 3.20151124.1) it seems you can just do xdotool getmouselocation --shell while hovering the window you need to work on: https://mcmap.net/q/369958/-how-would-i-get-the-current-mouse-coordinates-in-bashAlbina
P
3

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".)

Pitta answered 14/1, 2016 at 18:46 Comment(0)
M
7

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
Mourner answered 10/5, 2020 at 0:42 Comment(0)
P
3

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".)

Pitta answered 14/1, 2016 at 18:46 Comment(0)
P
0

If you are running a linux system and are using x-display manager, the command xwininfo to get window information may work for you.

You can run or script the command xwininfo -root -tree and get a detailed and organized output of the xwindows system and the window IDs. You can see how I have used the xwininfo output in a small bot I created for runescape. I store the hexidecimal ID of the window title and then convert it to binary using printf %i and the hex of the window I am interested in. From there, I can script xdotool to do whatever I want in or with the window by making it the active window and moving the mouse to it using xdotool.

enter image description here

So the steps for this method are:

  1. Get the window information using xwininfo -root -tree.
  2. Store the window ID that you are interested in. (There may be multiple listings of the window name with different hex IDs and you will have to know the parent hex ID which is usually the first listed and commonly outlines the child processes in
    a sub-tree.)
  3. Convert the window hex ID into binary so xdotool can recognize it.
  4. Use the windowactivate option with xdotool in order to getxdotool to interact with the correct window.
  5. Perform whatever next steps you wish.

So it's really quite simple, although a bit time-consuming at first, but once you develop your own method for storing the parent window's hex ID, the rest can be easily automated and no worries arise. If you have trouble determining which hex ID belongs to the parent window, there are ways of checking for parent vs child processes but I don't know if you want to go into all of that. Just comment if you want me to include that information.

I hope this was helpful.

Profession answered 15/2, 2020 at 9:34 Comment(0)
R
0

@Yokai apparently gnome doesn't support xdotool

$ xdotool windowactivate "$window_ID_decimal"

Your windowmanager claims not to support _NET_ACTIVE_WINDOW, so the attempt to activate the window was aborted. xdo_activate_window on window:######## reported an error

Riflery answered 20/2, 2023 at 1:12 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewPhonsa

© 2022 - 2024 — McMap. All rights reserved.