如何在 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.com/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.com/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.