Python launches browser Firefox\chrome\ie

Source: Internet
Author: User


  1. #-*-Coding:utf-8-*-
  2. Import OS
  3. Import Selenium
  4. From selenium import Webdriver
  5. From Selenium.webdriver.common.keys import keys
  6. """
  7. Practice launching various browsers: Firefox, Chrome, IE
  8. Practice launching various browsers while loading plugins: Firefox, Chrome, IE
  9. """
  10. Def startfirefox ():
  11. "" "Launches the Firefox browser installed in the default location and automatically goes to Baidu homepage" "
  12. Driver = Webdriver. Firefox ()
  13. Driver.get ("http://www.baidu.com")
  14. ASSERT ("Baidu" in Driver.title)
  15. Elem = Driver.find_element_by_name ("WD")
  16. Elem.send_keys ("Selenium")
  17. Elem.send_keys (Keys.return)
  18. Assert "Baidu" in Driver.title
  19. Driver.close ()
  20. Driver.quit ()
  21. Driver = None
  22. Def startfirefoxwithspecificlocation ():
  23. "" "Launches the Firefox browser installed in a nondefault location and automatically goes to the Baidu homepage" "
  24. Firefoxbin = Os.path.abspath (r "C:\Program Files (x86) \mozilla Firefox\firefox.exe")
  25. os.environ["Webdriver.firefox.bin"] = Firefoxbin
  26. Driver = Webdriver. Firefox ()
  27. Driver.get ("http://www.baidu.com")
  28. ASSERT ("Baidu" in Driver.title)
  29. Elem = Driver.find_element_by_name ("WD")
  30. Elem.send_keys ("Selenium")
  31. Elem.send_keys (Keys.return)
  32. Assert "Baidu" in Driver.title
  33. Driver.close ()
  34. Driver.quit ()
  35. Driver = None
  36. Def startchrome ():
  37. "" Launches Chrome browser and automatically goes to Baidu homepage
  38. Start the Chrome browser to specify the location of the drive
  39. """
  40. Chrome_driver = Os.path.abspath 
  41. os.environ["Webdriver.chrome.driver"] = Chrome_driver
  42. Driver = Webdriver. Chrome (Chrome_driver)
  43. Driver.get ("http://www.baidu.com")
  44. ASSERT ("Baidu" in Driver.title)
  45. Elem = Driver.find_element_by_name ("WD")
  46. Elem.send_keys ("Selenium")
  47. Elem.send_keys (Keys.return)
  48. Assert "Baidu" in Driver.title
  49. Driver.close ()
  50. Driver.quit ()
  51. Driver = None
  52. Def startie ():
  53. "" "Start IE browser, and automatically go to Baidu homepage
  54. Start IE Browser To specify the location of the drive
  55. """
  56. Ie_driver = Os.path.abspath 
  57. os.environ["Webdriver.ie.driver"] = Ie_driver
  58. Driver = Webdriver. Ie (Ie_driver)
  59. Driver.get ("http://www.python.org")
  60. ASSERT ("Python" in Driver.title)
  61. Elem = driver.find_element_by_id ("Id-search-field")
  62. Elem.send_keys ("Selenium")
  63. ‘‘‘
  64. Elem.send_keys (Keys.return)
  65. Assert "Baidu" in Driver.title
  66. Driver.close ()
  67. Driver.quit ()
  68. Driver = None
  69. ‘‘‘
  70. Def start_firefox_with_firebug_plug ():
  71. "" "Start Firefox, and automatically load plugins firebug" ""
  72. Firefoxbin = Os.path.abspath (r "C:\Program Files (x86) \mozilla Firefox\firefox.exe")
  73. os.environ["Webdriver.firefox.bin"] = Firefoxbin
  74. Firefoxprofile = Webdriver. Firefoxprofile ()
  75. TempDir = OS.GETCWD ()
  76. TempDir = Os.path.split (TempDir) [0]
  77. Firebugplugfile = Os.path.join (Os.path.join (TempDir, "Files"), "Firebug-2.0.7.xpi")
  78. Firefoxprofile.add_extension (Firebugplugfile)
  79. Firefoxprofile.set_preference ("Extensions.firebug.currentVersion", "2.0.7")
  80. Driver = Webdriver. Firefox (Firefox_profile=firefoxprofile)
  81. Driver.get ("http://www.baidu.com")
  82. Def start_chrome_with_chrometomobile_plug ():
  83. "" "Launches chrome and automatically loads the plugin chrome to Mobile" "
  84. TempDir = OS.GETCWD ()
  85. TempDir = Os.path.split (TempDir) [0]
  86. Chrome_driver_file = Os.path.join (Os.path.join (TempDir, "Files"), "Chromedriver.exe")
  87. os.environ["Webdriver.chrome.driver"] = Chrome_driver_file
  88. Chrome_to_mobile_plug_file = Os.path.join (Os.path.join (TempDir, "Files"), "CHROME-TO-MOBILE_V3.3.CRX")
  89. Chrome_options = Webdriver. Chromeoptions ()
  90. Chrome_options.add_extension (Chrome_to_mobile_plug_file)
  91. Driver = Webdriver. Chrome (Executable_path=chrome_driver_file,
  92. Chrome_options=chrome_options)
  93. Driver.get ("http://www.baidu.com")
  94. ‘‘‘
  95. Driver.close ()
  96. Driver.quit ()
  97. Driver = None
  98. ‘‘‘
  99. Def start_firefox_with_default_settings ():
  100. "" To start the Firefox browser and configure the browser with the options in the local configuration file
  101. Automatically exports the page loading process to the Har file and stores it in
  102. Configuration item Extensions.firebug.netexport.defaultLogDir the specified D:\temp\selenium2 directory
  103. """
  104. Firefox_bin = Os.path.abspath (r "C:\Program Files (x86) \mozilla Firefox\firefox.exe")
  105. os.environ["Webdriver.firefox.bin"] = Firefox_bin
  106. # Use a browser configuration copied from another machine
  107. Firefox_profile = Webdriver. Firefoxprofile (Os.path.abspath (r "D:\Temp\selenium2\Profiles\mm9zxom8.default"))
  108. # using the local default configuration
  109. #firefox_profile = Webdriver. Firefoxprofile (r "C:\Users\eli\AppData\Roaming\Mozilla\Firefox\Profiles\mm9zxom8.default")
  110. Driver = Webdriver. Firefox (Firefox_profile=firefox_profile)
  111. Driver.get ("http://www.baidu.com")
  112. Driver.get ("http://www.baidu.com")
  113. ‘‘‘
  114. Driver.close ()
  115. Driver.quit ()
  116. Driver = None
  117. ‘‘‘
  118. Def start_chrome_with_default_settings ():
  119. "" To start the Firefox browser and configure the browser "" with the options in the local configuration file
  120. TempDir = OS.GETCWD ()
  121. TempDir = Os.path.split (TempDir) [0]
  122. Chrome_driver = Chrome_driver_file = Os.path.join (Os.path.join (TempDir, "Files"), "Chromedriver.exe")
  123. os.environ["Webdriver.chrome.driver"] = Chrome_driver
  124. Chrome_options = Webdriver. Chromeoptions ()
  125. Chrome_options.add_argument ("--test-type")
  126. #chrome_options. Add_argument ("user-data-dir=" +os.path.abspath (r "D:\Temp\selenium2\User data")
  127. Chrome_options.add_argument ("user-data-dir=" +os.path.abspath (r "C:\Users\eli\AppData\Local\Google\Chrome\User Data "))
  128. Driver = Webdriver. Chrome (Executable_path=chrome_driver,
  129. Chrome_options=chrome_options)
  130. Driver.get ("http://www.baidu.com")
  131. if __name__ = = "__main__":
  132. # 2. Plug-ins are loaded automatically when the browser is launched, such as Firefox-Firebug; Chrome to Mobile
  133. # Start_firefox_with_firebug_plug ()
  134. # Start_chrome_with_chrometomobile_plug ()
  135. # start_firefox_with_default_settings ()
  136. Start_chrome_with_default_settings ()
  137. # 1. Launch various browsers
  138. #startFirefox ()
  139. #startFirefoxWithSpecificLocation ()
  140. #startChrome ()
  141. #startIE ()


Python launches browser 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.