Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何自动获取xdotool的窗口ID_Linux_Bash_Scripting_Ps_Xdotool - Fatal编程技术网

Linux 如何自动获取xdotool的窗口ID

Linux 如何自动获取xdotool的窗口ID,linux,bash,scripting,ps,xdotool,Linux,Bash,Scripting,Ps,Xdotool,我正在尝试自动化测试selenium会花费太长时间的表单(javascript繁重的现代表单),我希望使用xdotool并获取窗口ID。我知道您可以调用xdool selectwindow并单击它,但每次都必须单击它。我想告诉它“对于谷歌chrome窗口,标签标题是x,DoY” 我在这里得到了窗口ID: cchilders@cchilders-Dell-Precision-M3800:~$ xdotool selectwindow 65011713 这是针对chrome本身的,单击每个选项卡时

我正在尝试自动化测试selenium会花费太长时间的表单(javascript繁重的现代表单),我希望使用xdotool并获取窗口ID。我知道您可以调用
xdool selectwindow
并单击它,但每次都必须单击它。我想告诉它“对于谷歌chrome窗口,标签标题是x,DoY”

我在这里得到了窗口ID:

cchilders@cchilders-Dell-Precision-M3800:~$ xdotool selectwindow
65011713
这是针对chrome本身的,单击每个选项卡时获得相同的值。所以我希望在ps或窗口管理器中找到,但没有:

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
ps也不起作用:

(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...
并获取第一个由空格分隔为int的数字:

In [13]: int("0x03e00001", 16)
Out[13]: 65011713
int中的16标志告诉它应该是十六进制

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'
[14]中的
:int(“0x03e00001”)
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
---->1整数(“0x03e00001”)
ValueError:基数为10的int()的文本无效:“0x03e00001”

您可以使用
awk
wmctrl-l
的输出中提取ID

例如:

wmctrl -l | awk '/Google Chrome/ {print $1}'
xdool
很可能会接受十六进制ID,但如果不能,您可以使用
strtonum
将其转换为十进制表示:

wmctrl -l | awk '/Google Chrome/ {print strtonum($1)}'
如何从
awk
中的输出中匹配所需的窗口取决于您和您的需求


可能值得注意的是,
xdool
似乎还有一个
search
命令,该命令使用各种类型的说明符和模式,您可以使用这些说明符和模式来获取要操作的窗口的窗口ID。(它甚至支持一堆匹配项,它支持一种特殊格式的“窗口ID”,可以直接对“链接命令”进行操作。)

如果您运行的是linux系统,并且正在使用x-display管理器,那么获取窗口信息的命令
xwininfo
可能适合您

您可以运行或编写命令
xwininfo-root-tree
,并获得xwindows系统和窗口ID的详细且有组织的输出。您可以看到我如何在为runescape创建的一个小bot中使用
xwininfo
输出。我存储窗口标题的十六进制ID,然后使用
printf%I
和我感兴趣的窗口的十六进制将其转换为二进制。从那里,我可以编写
xdool
脚本,通过将窗口设置为活动窗口并使用
xdool
将鼠标移动到窗口中,在窗口中或使用窗口执行任何操作

因此,该方法的步骤如下:

  • 使用
    xwininfo-root-tree
    获取窗口信息
  • 存储您感兴趣的窗口ID。 (可能有多个具有不同名称的窗口名称列表。) 十六进制ID,您必须知道父十六进制ID 通常在
    子树。)
  • 将窗口十六进制ID转换为二进制,以便
    xdool
    能够识别它
  • windowactivate
    选项与
    xdocool
    一起使用,以获取
    xdocool
    与正确的窗口交互
  • 执行您希望的任何后续步骤
  • 因此,它实际上非常简单,虽然一开始有点费时,但一旦您开发了自己的方法来存储父窗口的十六进制ID,其余的就可以轻松地自动化,而且不必担心。如果您在确定哪个十六进制ID属于父窗口时遇到困难,可以通过多种方法检查父进程与子进程,但我不知道您是否想了解所有这些。如果你想让我提供这些信息,请发表评论


    我希望这会有所帮助。

    您可以按名称、窗口类等查找包含xdotool的窗口。要按名称搜索窗口,请使用:

    xdotool search --name 'My Window Name'
    
    这将把十进制窗口id打印到标准输出。--name标志与部分或全部窗口名匹配。在浏览器中,通常包括当前选项卡名称。xdotool还可以返回相应的pid,如下所示:

    xdotool search --name 'My Window Title' getwindowpid
    
    他就是一个向窗口发送一系列按键和鼠标点击的例子

    # 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
    

    65011713
    是一个二进制值。从
    wmctrl
    返回的值是十六进制值。您需要进行转换以使它们匹配(虽然这三个都不匹配,但我在该列表中根本看不到
    chrome
    )。不过,
    ps
    中的任何内容都与此无关。如果您无法为选项卡获取单独的窗口ID(并且您可能无法),那么您可能无法专门针对某个选项卡(或者首先必须使用
    xdool
    查看选项卡)。(可能值得查看
    xwininfo-tree
    的输出,看看您是否可以在那里找到您要查找的内容。)是的,没错,这很麻烦,但如果您想让我选择它,可以将其作为一个问题使用。我几乎笑出了我的脾脏在花花公子投票结束这样一个具体的事情。你刚才用自己的答案更新了你的问题吗?使用selenium,你可以在选项卡之间切换。
    # 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