Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to wait till page reloads/redirects after clicking button #184

Closed
infalmo opened this issue Aug 10, 2020 · 14 comments
Closed

How to wait till page reloads/redirects after clicking button #184

infalmo opened this issue Aug 10, 2020 · 14 comments
Labels
question Questions related to rod

Comments

@infalmo
Copy link
Contributor

infalmo commented Aug 10, 2020

Currently, I do something like this:

page.Element(".submit").Click()
time.Sleep(time.Millisecond*300)
page.WaitLoad()
// do next stuff here

Is there any better way than this? One way I tried (but failed was)

wait := page.WaitRequestIdle()
page.Element(".submit").Click()
wait()
// do next steps ...
@infalmo infalmo added the question Questions related to rod label Aug 10, 2020
@infalmo infalmo changed the title Wait till page reloads/redirects after clicking submit button How to wait till page reloads/redirects after clicking submit button Aug 10, 2020
@infalmo infalmo changed the title How to wait till page reloads/redirects after clicking submit button How to wait till page reloads/redirects after clicking button Aug 10, 2020
@ysmood
Copy link
Collaborator

ysmood commented Aug 11, 2020

Can you provide minimal code for us to reproduce your issue?

@ysmood
Copy link
Collaborator

ysmood commented Aug 11, 2020

If a page is like this, I want to click the button, how can we define what is page loaded?

<html>
    <body>
        
    </body>

    <script>
        setTimeout(() => {
            document.body.innerHTML = '<button>click</button>'
        }, 1000 * randomSeconds)
    </script>
</html>

In this case, the race selectors is the only choice for me.

@ysmood ysmood added the needs info The description is not enough to tackle the problem label Aug 11, 2020
@infalmo
Copy link
Contributor Author

infalmo commented Aug 11, 2020

Here is the entire code.

page := Browser.Page("https://codeforces.com/enter").WaitLoad()
page.Element("#handleOrEmail").Input(usr)
page.Element("#password").Input(passwd)
page.Element(".submit").Click()
time.Sleep(time.Millisecond*300)
page.WaitLoad()

// Check if in the home page now
page.Screenshot("")

@ysmood ysmood removed the needs info The description is not enough to tackle the problem label Aug 11, 2020
@ysmood
Copy link
Collaborator

ysmood commented Aug 11, 2020

Don't forget to log our conversation in discord 😃

@infalmo
Copy link
Contributor Author

infalmo commented Aug 11, 2020

Thanks for the answer @ysmood.
For those interested, the answer is running running a race selector page.ElementMatches() for some unique value in either of the 2 pages. In this case, it is page.ElementMatches("a", "Logout", "span", "Invalid handle/email or password") which runs a regex search on the select class.

@infalmo infalmo closed this as completed Aug 11, 2020
@zplzpl

This comment has been minimized.

@zplzpl

This comment has been minimized.

@zplzpl

This comment has been minimized.

@infalmo
Copy link
Contributor Author

infalmo commented Feb 27, 2021

You have 2 options:

  1. explicitly wait for some particular element on login (page.MustElementR("a", "Welcome") for example, which is present only once logged in).

  2. Wait till page loads after clicking the button:

page.MustElement(".submit").MustClick().MustWaitInvisible()
page.MustWaitLoad()

Option 1 is what you'd aim to do, if the web page doesn't reload on submit. Plus it's faster, if you know exactly what you're searching for. Option 2 is suitable for all other cases.

@infalmo
Copy link
Contributor Author

infalmo commented Feb 27, 2021

Also, the API changed a lot since when this issue was filed, you are looking for the method page.HasR()

@zplzpl

This comment has been minimized.

@ysmood
Copy link
Collaborator

ysmood commented Feb 27, 2021

@zplzpl If you are not comfortable using English please use https://www.deepl.com/ to translate your mother tongue. Your words don't make sense to me 😂

Please raise another issue for your question, yours is not related to the current one. Also, please read this #322.

@zplzpl
Copy link

zplzpl commented Feb 27, 2021

@zplzpl If you are not comfortable using English please use https://www.deepl.com/ to translate your mother tongue. Your words don't make sense to me 😂

Please raise another issue for your question, yours is not related to the current one.

Thanks for the suggestion, I'll reopen an issue.

@tim0991
Copy link

tim0991 commented Jul 12, 2022

if page redirect by js with settimeout u can do this

for {
        // if u find a nonexistent elment by Elment directly will hang up even with out for{}
        // so u must use Has before Element
	if page.MustHas("#error_display") {
		fmt.Println("loginErr")
		break
	}
	info := page.MustInfo()
	if strings.HasPrefix(info.URL, "other domain") {
		fmt.Println("loginSuccess")
		break
	}
	time.Sleep(time.Second)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions related to rod
Projects
None yet
Development

No branches or pull requests

4 participants