-
- #-*-Coding:utf-8-*-
-
- Import OS
-
- Import Selenium
-
- From selenium import Webdriver
-
- From Selenium.webdriver.common.keys import keys
-
-
-
- """
-
- Practice launching various browsers: Firefox, Chrome, IE
-
- Practice launching various browsers while loading plugins: Firefox, Chrome, IE
-
- """
-
-
-
- Def startfirefox ():
-
- "" "Launches the Firefox browser installed in the default location and automatically goes to 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 startfirefoxwithspecificlocation ():
-
- "" "Launches the Firefox browser installed in a nondefault location and automatically goes 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 ():
-
- "" Launches Chrome browser and automatically goes to Baidu homepage
-
- Start the Chrome browser to specify the location of the drive
-
- """
-
- 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
-
- Start IE Browser To specify the location of the drive
-
- """
-
- 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 plugins 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 ():
-
- "" "Launches chrome and automatically loads the plugin 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 ():
-
- "" To start the Firefox browser and configure the browser with the options in the local configuration file
-
- Automatically exports the page loading process to the Har file and stores it in
-
- Configuration item Extensions.firebug.netexport.defaultLogDir the specified 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 a browser configuration copied from another machine
-
- Firefox_profile = Webdriver. Firefoxprofile (Os.path.abspath (r "D:\Temp\selenium2\Profiles\mm9zxom8.default"))
-
- # using 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 ():
-
- "" To start the Firefox browser and configure the browser "" with the options in the local configuration file
-
- 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. Plug-ins are loaded automatically when the browser is launched, such as Firefox-Firebug; Chrome to Mobile
-
- # Start_firefox_with_firebug_plug ()
-
- # Start_chrome_with_chrometomobile_plug ()
-
- # start_firefox_with_default_settings ()
-
- Start_chrome_with_default_settings ()
-
-
-
-
-
- # 1. Launch various browsers
-
- #startFirefox ()
-
- #startFirefoxWithSpecificLocation ()
-
- #startChrome ()
-
- #startIE ()
Python launches browser Firefox\chrome\ie