CodeLover
CodeLover

Reputation: 1074

How to refresh a page on browser using selenium-webdriver?

Follow the below part of the code:

driver.get "https://example.com/"

element = driver.find_element :name => "username"
element.send_keys "*****"
element = driver.find_element :name => "password"
element.send_keys "*****"
element.submit

Now point is when the driver opened the URL on the browser, Some times the next page to which username and password has to be put by the script, not coming- which causes the script to failed. Some intermediate page is coming asking us to click on 'retry button' or 'refresh the page'. Thus the script whenever such case occurred stopped execution, as not getting the mentioned element.

So is there any way to refresh that intermediate page with "sleep" before going to the "Log-in" page, so that script can run in one go?

Upvotes: 32

Views: 59473

Answers (7)

Justin Ko
Justin Ko

Reputation: 46836

You can use the Navigation#refresh method:

driver.navigate.refresh

Upvotes: 27

Percy
Percy

Reputation: 1057

For Capybara, I think Tommyixi's comment is the best:

visit current_path

Upvotes: 36

Dan Kohn
Dan Kohn

Reputation: 34337

As mentioned in a comment by @Nakilon, with non-Poltergeist drivers in Capybara use the following to refresh the current page:

page.driver.browser.navigate.refresh

But for a universal fix, use:

page.evaluate_script 'window.location.reload()'

Upvotes: 26

moritz
moritz

Reputation: 25767

For capybara, I recommend

page.evaluate_script("window.location.reload()")

This also works with poltergeist/phantomjs

Upvotes: 14

Varun
Varun

Reputation: 31

You can use - driver.navigate().refresh();.

Look here for example - http://reditblog.blogspot.in/2014/10/how-to-refresh-current-web-page.html

Upvotes: 0

gstroup
gstroup

Reputation: 1064

In cases like this, I've written some code to check for the existence of the input field. If it doesn't exist, then refresh.

driver.refresh()

Upvotes: 6

Jim Stewart
Jim Stewart

Reputation: 17323

Edit: my original answer assumed Capybara was being used.

You can just use driver.get on the page again to refresh. There's no difference between clicking the refresh button in a browser and typing the same URL to load the page again, so driver.get is sufficient for this behavior.

Upvotes: 1

Related Questions