トップページ > Selenium API(逆引き) > 【Java】refresh

【Java】refresh・・・ブラウザを更新する

【PR】業界トップクラスの「高単価報酬」「低マージン」を実現!レバテックフリーランス

「refresh」はブラウザの更新(F5キー押下)と同じ動作をします。

「refresh」を実行することで、確定されていない値や未保存の値はクリアされます。また、リアルタイムの情報などは「refresh」を実行することで、値が新たに更新されます。

更新はカレントページに対して行っていきます。

説明

◆メソッド
  ・void refresh()
◆使用形態
  ・driver.navigate().refresh()
◆備考
  ・ブラウザのリフレッシュと同様の動作をする
◆関連項目
  ・クッキーを指定して削除する
  ・全てのクッキーを削除する

サンプル

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebTest{
  public static void main(String[] args){
    //Chrome制御のためChromeDriverのパスを指定
	System.setProperty("webdriver.chrome.driver",
		                            "D:\\AutoTest\\chromedriver.exe");
	//Chromeを起動する
	WebDriver driver = new ChromeDriver();
	//Googleページに移行
    driver.get("https://www.google.co.jp");
    //10秒間のDurationを設定
    Duration waitTime = Duration.ofSeconds(10);
    //ページが完全にロードされるまで待機
    driver.manage().timeouts().pageLoadTimeout(waitTime);
    //検索テキストボックスの要素を取得
    WebElement element = driver.findElement(By.name("q"));
    //検索テキストボックスに"Test"と入力
    element.sendKeys("Test");
	//「Test」とテキストボックスに入力されていることを確認
	try{
	   Thread.sleep(5000);
	 }catch (InterruptedException e){
   }
    //ページを更新(ブラウザのリフレッシュボタン)
    driver.navigate().refresh();
  }
}
※selenium version 4.1.4で動作確認をしています(ブラウザのUIや属性値などが変更された場合、実行結果が異なる可能性があります)

実行結果

refresh
 ※GoogleおよびGoogleロゴはGoogle Inc.の登録商標であり、同社の許可を得て使用しています。

動画デモ

※字幕をONにすると解説のテロップが表示されます※


Fatal error: Uncaught Google_Service_Exception: Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A138132118&start-date=7daysAgo&end-date=yesterday&metrics=ga%3Apageviews&dimensions=ga%3ApageTitle%2C+ga%3ApagePath&sort=-ga%3Apageviews&max-results=1000: (404) <!DOCTYPE html> <html lang=en> <meta charset=utf-8> <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"> <title>Error 404 (Not Found)!!1</title> <style> *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_c in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php on line 110