Practice launching various browsers while loading plugins: Firefox, Chrome, IE

Source: Internet
Author: User



#-*-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 the Baidu home page" "
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 ():
"" "Start installation on non- Default location of Firefox browser, and automatically go to Baidu home "" "
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 home
to start the Chrome browser needs to specify the location of the drive
" ""
Chrome_driver = Os.path.abspath 

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 need to specify the location of the driver
" "
Ie_driver = Os.path.abspath
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 ():
"" "Launches Firefox and automatically loads plug-ins firebug" ""
Firefoxbin = Os.path.abspath (r "\ \ 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 plugins 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 ()


Practice launching various browsers while loading plugins: Firefox, Chrome, IE


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.