0

I am doing a project which behaves like autologin using xdotool. Below is the bash script command:

if [ "$url" == "https://github.com/login" ]; then
  sleep 5
  xdotool type $WUSER
  xdotool key Tab
  xdotool type $DECPASS
  xdotool key Return
else 
  exit 1
fi

There will be a default URL for login page (eg: https://github.com/login), which will run this script below on the browser startup:

  • automatically type in the username
  • press tab key
  • type in the password
  • click enter

At the moment I use sleep 5 (wait 5 seconds until running the next command) which is a bit hacky because some pages load really fast and others don't.

Question

How to check first if the page is fully loaded before running the command? Maybe it will look something like this, or if there's some other better methods.

if [ "$url" == "https://github.com/login" ]; then
  if [ <page is fully loaded> ]; then
     xdotool type $WUSER
     xdotool key Tab
     xdotool type $DECPASS
     xdotool key Return
  else
     <wait until page loads>
  fi
else 
  exit 1
fi
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Faiz
  • 3
  • 1
  • 2
    bash is the wrong tool for this job. Use Selenium. – Charles Duffy Dec 01 '21 at 14:08
  • 1
    I confirm that selenium is a more suitable framework that you can use with the programming language of your choice. If you give it a try, have a look at [expected conditions](https://www.selenium.dev/fr/documentation/webdriver/waits/#explicit-wait) to solve your problem. – Sylvan LE DEUNFF Dec 01 '21 at 19:15

1 Answers1

0

This script's logic tries to find if login was successful, instead of trying other things first. This makes it easy for in bash, because other solutions (in bash) are complicated or undoable. While all these may be great, they can make it very complicated for you.

Instead you want to make it simple for you.. then try this:

sleep 5;
if [[ <check with xdotool search --name for a window to confirm login> ]];
echo " Window exists, login was successful;
else
echo "Login failed, retring.
<Repeat login>
fi;

For instance, I know when I (sucessfully )login to Github, there is a page titled "Github". Before login, there is a page "Sign in to Github"

So may be i can check if sign in to github page still exists.

#so you'r script will look like this
#keep trying to login, til successs
while true;
do
  sleep 5;
  #your code to Log-in
  if [[ -z "$(xdotool search --name "Sign in to GitHub")" ]];
  then
    echo "I reckon login was succesfful.";
    break 1;
    else
    echo "Try again.";
  fi;
done;

For the above to work, the Browser window must be open in a separate window or the Tab should be the visible one.