📜  打印 url selenium python (1)

📅  最后修改于: 2023-12-03 14:54:27.270000             🧑  作者: Mango

打印 URL using Selenium in Python

Introduction

In this guide, we will learn how to use Selenium in Python to print the URL of a webpage. Selenium is a powerful tool for web scraping and automated testing. By using Selenium, we can automate the process of opening a webpage and extracting its URL.

Prerequisites

Before we begin, make sure you have the following:

  • Python installed on your machine
  • Selenium library installed (pip install selenium)
  • Chrome browser installed (as we will use Chrome as our WebDriver)
Implementation

Follow the steps below to print the URL of a webpage using Selenium in Python:

1. Import the required modules
from selenium import webdriver
2. Set up the WebDriver
driver = webdriver.Chrome()
3. Open the webpage
driver.get("https://example.com")
4. Print the URL
print(driver.current_url)
5. Close the WebDriver
driver.quit()
Example

Here is a complete example that demonstrates how to print the URL of a webpage using Selenium in Python:

from selenium import webdriver

# Set up the WebDriver
driver = webdriver.Chrome()

# Open the webpage
driver.get("https://example.com")

# Print the URL
print(driver.current_url)

# Close the WebDriver
driver.quit()
Conclusion

Using the above guide, you can easily print the URL of a webpage using Selenium in Python. Selenium provides a convenient way to automate web interactions and extract information from websites.