Jump to content
UBot Underground

How to create loop until specific success message is detected on the page?


Recommended Posts

I wrote a newbie-quality bot that needs to be improved, but I don't know how to do it.

 

Many times, there are errors preventing a successful submission. Either the page code didn't load properly and the "submit" button is dead (nothing happens when it's clicked, it's dead, but a reload fixes this), or the captcha will be entered wrong causing a submission error. Either way, the the script will click submit and move on to the next log in, not knowing if the submit button didn't do anything, or if the captcha was wrong.

 

If the submit button did in fact work, the page will reload with either an error or success message (based on the captcha being accurate or not). Either way, the bot will continue on, logging out as the current user, then logging in as the next user.

 

If the submit button didn't work, or if the captcha was entered wrong, the submission is never counted and the bot continues (I have to pause my bot, manually reload the page, make my selection enter the captcha, and click submit manually, and then resume the bot).

 

I would like to figure out a way to make my bot smarter, whereby it first detects (maybe by a timeout) if the submit button doesn't work and the page didn't reload with a status message, in which case it will manually reload and reselect, reenter captcha, and click submit again. If the submit button did work, the bot would need to read the red text status message after submit is clicked to determine if the submission was done correctly ("thank you...") or incorrectly ("please re enter the captcha..."). If the success message appears, proceed as normal. If the error message appears, redo the selection, captcha and submission and try to get a success message again. repeat. 

 

Any ideas on how to do this properly? I understand the basics of ubot, but not enough to know how to build this.

 

Thank you!

Link to post
Share on other sites

The general principle is to always look for success; for example after submit a page you would be looking for something that is only on the success page; if bot won't be able to find success element, it would know that he isn't on the right page. For bot to react properly you need to add some code that will handle that exception

 

Instead of success sometimes it's better to check if something is still displayed on page, like captcha, while still waiting for success to show up.

 

Here is an example code for solving captchas:

set(#CAPTCHA Sovled, $false, "Global")
set(#CAPTCHA Count, 0, "Global")
loop while($both($not(#CAPTCHA Sovled), $comparison(#CAPTCHA Count, "<", 3))) {
    change attribute(<name="recaptcha_response_field">, "value", $solve captcha(<src=w"*google.com/recaptcha/api/image?c=*">))
    click(<value="Submit">, "Left Click", "No")
    wait for browser event("Everything Loaded", "")
    wait for element(<id="SUCCESS">, 10, "Appear")
    if($exists(<src=w"*google.com/recaptcha/api/image?c=*">)) {
        then {
            increment(#CAPTCHA Count)
        }
        else {
            set(#CAPTCHA Sovled, $true, "Global")
        }
    }
}
if($exists(<id="SUCCESS">)) {
    then {
        alert("We reached success page.")
    }
    else if($exists(<src=w"*google.com/recaptcha/api/image?c=*">)) {
        alert("Captcha solving failed with count:{#CAPTCHA Count}.")
        stop script
    }
    else {
        alert("Failed to reach success page.")
        stop script
    }
}

In the example above you can see that bot will try to solve captcha 5 times, and will let you know if captcha still wasn't solved. However, if you fail to many times usually you will want to pause/stop the bot because something could be wrong with your code.

 

I hope this example illustrates how you should approach handling the unexpected situations.

Link to post
Share on other sites

Thank you for the reply. In my case, the captcha is present on every page, even if a user isn't logged in. It's always there, even when it isn't meant to be used. It may make more sense for me to search for the success element, as it is the only thing that differentiates success (or error) from a normal page. Any advice on how to find that specific text on the screen and if not, go thru the loop again? It would be good to limit this to 5 tries or so, then move on. 

 

As I mentioned earlier, I would also need to include code that can detect if the submit button isn't working (after captcha is solved), in which case I'd need to reload the page and start over. 

 

Thanks. 

Link to post
Share on other sites

If captcha is always displayed then use "<id="SUCCESS">" to determine if it was solved or not (the script above uses captcha image, since usually captchas are not displayed on every page).

 

After clicking the submit button you should do similar...check if <id="SUCCESS"> exist, if not, you can check if the form still exist, and if exist, you can re-fill and re-submit...

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...