This section describes how to initialize a Webdriver instance object driver, and then turn on and off the Firefox browser. To open the Fiefox browser with selenium. First need to download a driver plug-in geckodriver.exe, download the address https://github.com/mozilla/geckodriver/releases, download the exe file, Put this file in your Python installation directory, for example: C:\Python27\geckdriver.exe.
Open a blank file with notepad++ and enter the following code to save as first.py
From selenium import webdriver # import webdriver package
Driver = Webdriver. Firefox () # Initializes an instance of Firefox: Driver
Driver.maximize_window () # Maximize browser
driver.get ("https:// Www.baidu.com ") # Opens a URL site driver.quit () through the Get () method #关闭并退出浏览器
Open the CMD window, CD to first.py the path, in cmd input command: Python first.py carriage return, you can see whether to open Firefox, open Baidu, turn off the effect of Firefox.
Note: Some errors may be encountered
1. Geckodriver.exe is not placed in the Python installation directory, this type of error.
2. After the Python first.py was executed, the file was found, stating that no CD command was used to switch to the folder path where first.py resides.
Summarize:
The above code, and this article, have a basic understanding of how to open and close the browser. If you want to open IE or Chrome browser, you also need to download the corresponding browser Driver.exe file, put it in the Python installation directory.