ruby 如何使用 selenium-webdriver 在浏览器上刷新页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14509336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:41:21  来源:igfitidea点击:

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

rubyselenium-webdriver

提问by CodeLover

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 driveropened 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.

现在点是当在driver浏览器上打开 URL 时,有时脚本必须将用户名和密码放入下一个页面,而不是出现 - 这会导致脚本失败。一些中间页面即将出现,要求我们单击“重试按钮”或“刷新页面”。因此,只要发生这种情况,脚本就会停止执行,因为没有获得提到的元素。

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?

那么有没有办法在进入“登录”页面之前用“睡眠”刷新中间页面,以便脚本可以一次性运行?

采纳答案by Justin Ko

You can use the Navigation#refreshmethod:

您可以使用以下Navigation#refresh方法:

driver.navigate.refresh

回答by Dan Kohn

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

正如@Nakilon 在评论中提到的,Capybara 中的非 Poltergeist 驱动程序使用以下内容刷新当前页面:

page.driver.browser.navigate.refresh

But for a universal fix, use:

但是对于通用修复,请使用:

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

回答by Percy

For Capybara, I think Tommyixi's commentis the best:

对于水豚,我认为Tommyixi的评论是最好的:

visit current_path

回答by moritz

For capybara, I recommend

对于水豚,我推荐

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

This also works with poltergeist/phantomjs

这也适用于 poltergeist/phantomjs

回答by gstroup

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()

回答by Jim Stewart

Edit: my original answer assumed Capybara was being used.

编辑:我最初的回答假设正在使用 Capybara。

You can just use driver.geton 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.getis sufficient for this behavior.

您只需driver.get再次在页面上使用即可刷新。单击浏览器中的刷新按钮与输入相同的 URL 以再次加载页面之间没有区别,因此driver.get对于这种行为就足够了。

回答by Varun

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

您可以使用 - driver.navigate().refresh();

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

看看这里的例子 - http://reeditblog.blogspot.in/2014/10/how-to-refresh-current-web-page.html