Is there any way to fetch the title of a chromium-browser window via script or command?

152 Views Asked by At

I'm working on a project using Libre LePotato with Raspbian for a basic display kiosk. It launches chromium-browser to a specific url, then uses xdotool to type the username and password and hit enter. Once logged in the page refreshes every 5 minutes. Eventually the page forgets the login credentials and needs to be logged into again. The URL does not change between the login screen and the actual display page, but the title of the window does. Is there any way for me to fetch the title of the window? That way I can just create an if/then script that runs xdotool when the window title is Login.

I've tried setting the device up to reboot more frequently in order to hopefully reset the memory cache and log in again on boot up, but the issue occurs too frequently and constant rebooting on a display does not look good.

1

There are 1 best solutions below

0
hyperupcall On

The proper way to automate this would be through the use of Selenium, Playwright or Puppeteer (I prefer Puppeteer). If you insist though, you can use wmctrl.

For example, I'm using Brave, and let's say the normal output is this (I use Brave Browser):

$ wmctrl -l | grep -i brave
...
0x05400003  0 nullptr Home | Codewars - Brave
...

It lists the window id, a bunch of other stuff along with the title.

Now, the following is the output, when I'm on the sign-in page:

$ wmctrl -l | grep -i brave
...
0x05400b8b  0 nullptr Sign in | Codewars - Brave
...

With that knowledge, creating a script is relatively simple:

#!/usr/bin/env

if wmctrl -l | grep -q 'Sign in | Codewars - Brave'; then
  printf '%s\n' 'about to sign in'
else
  printf '%s\n' 'not signing in'
fi

Since this is all in a Kiosk, I figure that you won't have any other windows that potentialy interfere with the logic (ex. two codewars windows open at once)