📜  driver.get close how (1)

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

Introduction to driver.get and driver.close

Overview

When it comes to web automation, the driver.get and driver.close methods are two of the most commonly used functions. In this article, we will discuss what these functions are, how they work, and how you can use them in your Python code.

driver.get

The driver.get method is used to navigate to a specific URL in a web browser. It takes one parameter, which is the URL you want to navigate to. Here is an example of how to use driver.get:

from selenium import webdriver

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

# Navigate to google.com
driver.get("https://www.google.com")

As you can see, we first import the webdriver module from the selenium package. Next, we create a new instance of the Firefox web driver using webdriver.Firefox(). Finally, we use driver.get to navigate to the Google homepage.

driver.close

The driver.close method is used to close the current window or tab of the web browser. If there is only one window or tab open, the entire browser will be closed. Here is an example of how to use driver.close:

from selenium import webdriver

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

# Navigate to google.com
driver.get("https://www.google.com")

# Close the current window
driver.close()

As you can see, we simply call driver.close after navigating to Google. This will close the current window or tab.

Conclusion

In summary, driver.get is used to navigate to a specific URL in a web browser, while driver.close is used to close the current window or tab. These functions are essential for web automation, and with the help of the selenium package, you can easily automate repetitive tasks on the web.