Python + selenium start the browser Firefox \ Chrome \ IE

Source: Internet
Author: User
Tags assert

When writing a function, I was originally prepared to use the webbroswer module for implementation. However, I found that it can only be opened in a simple way, and the tab page cannot be opened. Therefore, it can be implemented through the selenium module. The following lists several functions defined by selenium to operate on different browsers.

1. webbroswer module

This module is very simple, as follows:

Import webbrowser
C = webbrowser. get ('Firefox ')
C. open_new ('http: // www.111cn.net ')
C. open_new_tab ('http: // www.111cn.net ')
During execution, we found that the open_new and open_new_tab operations were the same.

II. selenium common browser functions

#-*-Coding: UTF-8 -*-
Import OS
Import selenium
From selenium import webdriver
From selenium. webdriver. common. keys import Keys
"""
Start various browsers: Firefox, Chrome, and IE
Start various browsers and load plug-ins: Firefox, Chrome, and IE.
"""
Def startFirefox ():
"Start the Firefox browser installed in the default location and automatically go to the Baidu homepage """
Driver = webdriver. Firefox ()
Driver. get ("http://www.baidu.com ")
Assert ("Baidu" in driver. title)
Elem = driver. find_element_by_name ("wd ")
Elem. send_keys ("selenium ")
Elem. send_keys (Keys. RETURN)
Assert "Baidu" in driver. title
Driver. close ()
Driver. quit ()
Driver = None
Def startfirefoxwitheat ecificlocation ():
"Start the Firefox browser installed in a non-default location and automatically go to the Baidu homepage """
FirefoxBin = OS. path. abspath (r "C: \ Program Files (x86) \ Mozilla Firefox \ firefox.exe ")
OS. environ ["webdriver. firefox. bin"] = firefoxBin
Driver = webdriver. Firefox ()
Driver. get ("http://www.baidu.com ")
Assert ("Baidu" in driver. title)
Elem = driver. find_element_by_name ("wd ")
Elem. send_keys ("selenium ")
Elem. send_keys (Keys. RETURN)
Assert "Baidu" in driver. title
Driver. close ()
Driver. quit ()
Driver = None
Def startChrome ():
"Starts the Chrome browser and automatically goes to the Baidu homepage
To start Chrome, you must specify the driver location.
"""
Chrome_driver = OS. path. abspath
OS. environ ["webdriver. chrome. driver"] = chrome_driver
Driver = webdriver. Chrome (chrome_driver)
Driver. get ("http://www.baidu.com ")
Assert ("Baidu" in driver. title)
Elem = driver. find_element_by_name ("wd ")
Elem. send_keys ("selenium ")
Elem. send_keys (Keys. RETURN)
Assert "Baidu" in driver. title
Driver. close ()
Driver. quit ()
Driver = None
Def startIE ():
"Start IE browser and automatically go to Baidu homepage
To start the IE browser, you must specify the driver location.
"""
Ie_driver = OS. path. abspath
OS. environ ["webdriver. ie. driver"] = ie_driver
Driver = webdriver. Ie (ie_driver)
Driver. get ("http://www.python.org ")
Assert ("Python" in driver. title)
Elem = driver. find_element_by_id ("id-search-field ")
Elem. send_keys ("selenium ")
'''
Elem. send_keys (Keys. RETURN)
Assert "Baidu" in driver. title
Driver. close ()
Driver. quit ()
Driver = None
'''
Def start_firefox_with_firebug_plug ():
"Start Firefox and automatically load the plug-in Firebug """
FirefoxBin = OS. path. abspath (r "C: \ Program Files (x86) \ Mozilla Firefox \ firefox.exe ")
OS. environ ["webdriver. firefox. bin"] = firefoxBin
FirefoxProfile = webdriver. FirefoxProfile ()
TempDir = OS. getcwd ()
TempDir = OS. path. split (tempDir) [0]
FirebugPlugFile = OS. path. join (OS. path. join (tempDir, "Files"), "firebug-2.0.7.xpi ")
FirefoxProfile. add_extension (firebugPlugFile)
FirefoxProfile. set_preference ("extensions. firebug. currentVersion", "2.0.7 ")
Driver = webdriver. Firefox (firefox_profile = firefoxProfile)
Driver. get ("http://www.baidu.com ")
Def start_chrome_with_chrometomobile_plug ():
"Starts Chrome and automatically loads the plug-in Chrome to Mobile """
TempDir = OS. getcwd ()
TempDir = OS. path. split (tempDir) [0]
Chrome_driver_file = OS. path. join (OS. path. join (tempDir, "Files"), "chromedriver.exe ")
OS. environ ["webdriver. chrome. driver"] = chrome_driver_file
Chrome_to_mobile_plug_file = OS. path. join (OS. path. join (tempDir, "Files"), "Chrome-to-Mobile_v3.3.crx ")
Chrome_options = webdriver. ChromeOptions ()
Chrome_options.add_extension (chrome_to_mobile_plug_file)
Driver = webdriver. Chrome (executable_path = chrome_driver_file,
Chrome_options = chrome_options)
Driver. get ("http://www.baidu.com ")
'''
Driver. close ()
Driver. quit ()
Driver = None
'''
Def start_firefox_with_default_settings ():
"Start the Firefox browser and use the options in the local configuration file to configure the browser
Automatically export the page loading process as a Har file and save it in
Configuration item extensions. firebug. netexport. defaultLogDir specified by D: \ temp \ selenium2 Directory
"""
Firefox_bin = OS. path. abspath (r "C: \ Program Files (x86) \ Mozilla Firefox \ firefox.exe ")
OS. environ ["webdriver. firefox. bin"] = firefox_bin
# Use the browser configuration copied from another machine
Firefox_profile = webdriver. FirefoxProfile (OS. path. abspath (r "D: \ Temp \ selenium2 \ Profiles \ mm9zxom8. default "))
# Use the local default configuration
# Firefox_profile = webdriver. FirefoxProfile (r "C: \ Users \ eli \ AppData \ Roaming \ Mozilla \ Firefox \ Profiles \ mm9zxom8. default ")
Driver = webdriver. Firefox (firefox_profile = firefox_profile)
Driver. get ("http://www.baidu.com ")
Driver. get ("http://www.baidu.com ")
'''
Driver. close ()
Driver. quit ()
Driver = None
'''
Def start_chrome_with_default_settings ():
"Start the Firefox browser and use the options in the local configuration file to configure the browser """
TempDir = OS. getcwd ()
TempDir = OS. path. split (tempDir) [0]
Chrome_driver = chrome_driver_file = OS. path. join (OS. path. join (tempDir, "Files"), "chromedriver.exe ")
OS. environ ["webdriver. chrome. driver"] = chrome_driver
Chrome_options = webdriver. ChromeOptions ()
Chrome_options.add_argument ("-- test-type ")
# Chrome_options.add_argument ("user-data-dir =" + OS. path. abspath (r "D: \ Temp \ selenium2 \ User Data "))
Chrome_options.add_argument ("user-data-dir =" + OS. path. abspath (r "C: \ Users \ eli \ AppData \ Local \ Google \ Chrome \ User Data "))
Driver = webdriver. Chrome (executable_path = chrome_driver,
Chrome_options = chrome_options)
Driver. get ("http://www.baidu.com ")
If _ name _ = "_ main __":
#2. Automatically load the plug-in when starting the browser, such as Firefox-> Firebug; Chrome-> Chrome to Mobile
# Start_firefox_with_firebug_plug ()
# Start_chrome_with_chrometomobile_plug ()
# Start_firefox_with_default_settings ()
Start_chrome_with_default_settings ()
#1. Start various browsers
# StartFirefox ()
# StartFirefoxWithSpecificLocation ()
# StartChrome ()
# StartIE ()

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.