深度阅读

How to get title of a webpage using Selenium in Python?

作者
作者
2023年08月22日
更新时间
10.65 分钟
阅读时间
0
阅读量

To get the title of a webpage using Selenium in Python, you can use the title attribute of the WebDriver object that you have instantiated. Here’s an example:

from selenium import webdriver

# Create a new instance of the browser driver
driver = webdriver.Chrome()

# Navigate to the desired web page
driver.get('https://www.example.com')

# Get the title of the web page
title = driver.title

# Print the title to the console
print(title)

# Close the browser window
driver.quit()

In this example, we first create a new instance of the Chrome browser driver using the webdriver.Chrome() method. We then navigate to the desired webpage using the get() method, which loads the page in the browser window. Next, we use the title attribute of the WebDriver object to get the title of the webpage.

Finally, we print the title to the console using the print() function, and close the browser window using the quit() method of the WebDriver object.

You can also use similar code to extract other elements of the webpage using Selenium.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。