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

【Python】forward・・・ブラウザを履歴中で一段階進める

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

「forward」はブラウザの進むボタン押下した時と同じ動作をします。

「forward」を1度実行すると、現在のブラウザ表示から履歴を1つ進めた画面表示になります。また、履歴を1つ進めることでブラウザの戻るボタンが有効になります。

現在のブラウザ表示状態が、履歴中で一番新しい場合は「forward」を実行してもブラウザ表示状態はそのままで変化しません。

説明

◆メソッド
  ・forward()
◆使用形態
  ・driver.forward()
◆備考
  ・ブラウザ動作に依存しているためブラウザによっては、
        『進む』と違った動きをみせる可能性もある
◆関連項目
  ・ウィンドウのサイズを設定する
  ・ダイアログの[OK]ボタン/[Cancel]ボタンを押下する

サンプル

from selenium import webdriver
from selenium.webdriver.chrome import service
from selenium.webdriver.common.by import By

#ChromeDriverのパスを変数に設定
CHROMEDRIVER = "D:\driver\chromedriver.exe"
#ChromeDriverのstartとstopを制御するServiceオブジェクトを介してパスを渡す
chrome_service = service.Service(executable_path=CHROMEDRIVER)
#Chromeを起動
driver = webdriver.Chrome(service=chrome_service)
#Googleページに移行
driver.get("https://www.google.co.jp")
#リンクテキスト名が画像のリンク要素を取得
element = driver.find_element(By.LINK_TEXT, "画像")
#画像のリンクをクリックし画像検索ページへ移行
element.click()
#Googleページへ戻る(ブラウザを履歴中で一段階戻す)
driver.back()
#画像検索ページへ進む(ブラウザを履歴中で一段階進める)
driver.forward()
※selenium version 4.1.3で動作確認をしています(ブラウザのUIや属性値などが変更された場合、実行結果が異なる可能性があります)

実行結果

forward※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: (403) User does not have sufficient permissions for this profile. in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php:110 Stack trace: #0 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Task/Runner.php(174): Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php(46): Google_Task_Runner->run() #3 /home/users/1/monda-muki/web/seleniumqref in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php on line 110