如何在 Python 中使用 Selenium 在瀏覽器中執行後退和重新整理操作?


我們可以在 Selenium 中的瀏覽器中執行後退和重新整理操作。

要執行瀏覽器中的後退操作,需要使用 back 方法。

要重新整理瀏覽器,需要使用 refresh 方法。

這兩種方法都可以用於測試瀏覽器導航和網頁的重新載入。

示例

程式碼實現

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://tutorialspoint.tw/index.htm")
# to print the page title in console
print(driver.title)
# to print the current URL in console
print(driver.current_url)
#get method to launch another URL
driver.get("https://tutorialspoint.tw/questions/index.php")
# to go back to the previous URL
driver.back()
#to refresh the browser
driver.refresh()
#to close the browser
driver.close()

更新於: 2020 年 7 月 29 日

572 次瀏覽

開啟 職業生涯

完成課程以獲得認證

立即開始
廣告
© . All rights reserved.