我正在做一個專案,它的行為類似于使用 xdotool 的自動登錄。下面是 bash 腳本命令:
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
登錄頁面將有一個默認 URL(例如:https : //github.com/login),它將在瀏覽器啟動時運行以下腳本:
- 自動輸入用戶名
- 按tab鍵
- 輸入密碼
- 點擊進入
目前我使用 sleep 5(等待 5 秒直到運行下一個命令)這有點 hacky,因為有些頁面加載速度非常快,而有些則沒有。
題
如何在運行命令之前先檢查頁面是否已完全加載?也許它看起來像這樣,或者有其他更好的方法。
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
uj5u.com熱心網友回復:
此腳本的邏輯嘗試查找登錄是否成功,而不是先嘗試其他事情。這使得在 bash 中很容易,因為其他解決方案(在 bash 中)是復雜的或可撤銷的。盡管所有這些可能都很棒,但它們可能會讓您感到非常復雜。
相反,您想讓它變得簡單..然后試試這個:
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;
例如,我知道當我(成功)登錄到 Github 時,有一個標題為“Github”的頁面。登錄前,有一個頁面“登錄Github”
所以也許我可以檢查登錄到 github 頁面是否仍然存在。
#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;
要使上述作業正常運行,瀏覽器視窗必須在單獨的視窗中打開,或者 選項卡應該是可見的。
uj5u.com熱心網友回復:
如何在運行命令之前先檢查頁面是否已完全加載?
您應該使用curl來檢查回傳碼是否為 200。一旦回傳碼為 200,則繼續。
response=$(curl --write-out '%{http_code}' --silent --output /dev/null servername)
編輯:我看到這個問題之前已被問及回答。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/372411.html
上一篇:使用sed或任何其他命令將一行的一部分從file1復制到file2中的特定位置
下一篇:在bash中比較整數
