Python+selenium Basic 2-turn on and off Firefox browser __python

Source: Internet
Author: User


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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.