如何在 Selenium 中使用 Python 获取页面的标题和 URL?
我们可以在 Selenium 中获取页面的标题和 URL。
若要获取浏览器的标题,则需要使用 title 方法。
若要获取页面的 URL,则需要使用 current_url 方法。
如果我们需要测试是否导航到了正确的页面和页面标题,便可使用这两种方法。
实例
代码实现
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)
#to refresh the browser
driver.refresh()
#to close the browser
driver.close()
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP