text extraction from webpage

I’m trying to get text from a website login page to use as a loop controller since I know of no other way to check if the log in worked
This is my text

tell application "System Events"
	delay 10
end tell
repeat while loopcounter < item_num1
	tell application "Safari"
		activate
		open location site_url
		-- wait until page loaded
	end tell
	set filePath to (path to documents folder as Unicode text) & filepathend
	tell application "Finder" to delete (get files of folder filePath)
	tell application "System Events"
		delay 6
		
		keystroke tab & confirmationo & tab & "somename" & tab & "lastname" & return
		delay 4
		keystroke tab & space & tab & return
	end tell
	tell application "Safari"
		delay 5
		set pagetext to get text of document 1
		set ASTID_Priors to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "?"
		set interim_result to every text item of pagetext as list
		set newname to first item of interim_result
		set AppleScript's text item delimiters to " "
		set interim_result to every text item of newname as list
		if the last item of the result is "happened" then
			set loopcounter to loopcounter + 1
		else
			set loopcounter to 10
		end if
		
		set AppleScript's text item delimiters to ASTID_Priors
	end tell
	
end repeat

get pagetext with another script that just uses the webpage login error I get the text I can search but if I use this script the error login page just jumps back to the login page.
Why is it doing this?
How could I check the login error easier,
It returns a webpage with the subject “error”