Home > Blockchain >  How to check if a website is fully loaded in bash script?
How to check if a website is fully loaded in bash script?

Time:12-02

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

CodePudding user response:

This script logic tries to find if login was successful, instead of trying other things first. This makes it easy for in bash, because other solutions are complicated or undoable. While all these are great, they can make it very complicated. Instead you want to make it simple for you.. then try this:

sleep 5;
if [[ check with xdotool search --name for the window that loads after logging; if it window exists ]];
echo " Window exists, login was successful;
else
echo "Login failed, retring.
Repeat login;
fi;

CodePudding user response:

How to check first if the page is fully loaded before running the command?

You should use curl to check whether the return code has been 200. Once the return code is 200, proceed.

response=$(curl --write-out '%{http_code}' --silent --output /dev/null servername)

EDIT: I see this question has been asked an answered before.

  • Related