Python+selenium啟動瀏覽器Firefox\Chrome\IE

來源:互聯網
上載者:User

在寫一功能的時候,本來準備使用webbroswer模組實現,不過發現其只能實現簡單的開啟,而且開啟tab頁面發現並不能實現。所以決能通過selenium模組實現,這裡列下幾個selenium操作不同瀏覽器的定義的函數。

一、webbroswer模組

該模組的非常簡單,如下:

import webbrowser
c = webbrowser.get('firefox')
c.open_new('http://www.111cn.net')
c.open_new_tab('http://www.111cn.net')
執行的時候,發現open_new和open_new_tab操作效果一樣。

二、selenium操作常見瀏覽器函數

# -*- coding:utf-8 -*-
import os
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
"""
練習啟動各種瀏覽器:Firefox, Chrome, IE
練習啟動各種瀏覽器的同時載入外掛程式:Firefox, Chrome, IE
"""
def startFirefox():
    """啟動安裝在預設位置的Firefox瀏覽器,並自動轉到 百度 首頁"""
    driver = webdriver.Firefox()
    driver.get("http://www.baidu.com")
    assert("百度" in driver.title)
    elem = driver.find_element_by_name("wd")
    elem.send_keys("selenium")
    elem.send_keys(Keys.RETURN)
    assert "百度" in driver.title
    driver.close()
    driver.quit()
    driver = None
def startFirefoxWithSpecificLocation():
    """啟動安裝在 非 預設位置的Firefox瀏覽器,並自動轉到 百度 首頁"""
    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("百度" in driver.title)
    elem = driver.find_element_by_name("wd")
    elem.send_keys("selenium")
    elem.send_keys(Keys.RETURN)
    assert "百度" in driver.title
    driver.close()
    driver.quit()
    driver = None
def startChrome():
    """啟動Chrome瀏覽器,並自動轉到 百度 首頁
    啟動Chrome瀏覽器需要指定驅動的位置
    """
    chrome_driver = os.path.abspath(r"D:\雲端硬碟\360雲\360雲端硬碟\我的自動雙向同步資料夾\01-PersonalInfo\DataGuru\12-軟體自動化測試Selenium2\1-課程\練習代碼_Python版本\Selenium_python\Files\chromedriver.exe")
    os.environ["webdriver.chrome.driver"] = chrome_driver
    driver = webdriver.Chrome(chrome_driver)
    driver.get("http://www.baidu.com")
    assert("百度" in driver.title)
    elem = driver.find_element_by_name("wd")
    elem.send_keys("selenium")
    elem.send_keys(Keys.RETURN)
    assert "百度" in driver.title
    driver.close()
    driver.quit()
    driver = None
def startIE():
    """啟動IE瀏覽器,並自動轉到 百度 首頁
    啟動 IE 瀏覽器需要指定驅動的位置
    """
    ie_driver = os.path.abspath(r"D:\雲端硬碟\360雲\360雲端硬碟\我的自動雙向同步資料夾\01-PersonalInfo\DataGuru\12-軟體自動化測試Selenium2\1-課程\練習代碼_Python版本\Selenium_python\Files\IEDriverServer.exe")
    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 "百度" in driver.title
    driver.close()
    driver.quit()
    driver = None
    '''
def start_firefox_with_firebug_plug():
    """啟動Firefox,並自動載入外掛程式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():
    """啟動Chrome,並自動載入外掛程式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():
    """啟動Firefox瀏覽器, 使用本地設定檔中的選項配置瀏覽器
    自動將頁面載入過程匯出為Har檔案,並存放在
    配置項 extensions.firebug.netexport.defaultLogDir指定的D:\temp\selenium2目錄下
    """
    firefox_bin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
    os.environ["webdriver.firefox.bin"] = firefox_bin
    # 使用從別的機器上拷貝來的瀏覽器配置
    firefox_profile = webdriver.FirefoxProfile(os.path.abspath(r"D:\Temp\selenium2\Profiles\mm9zxom8.default"))
    # 使用本地的預設配置
    #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():
    """啟動Firefox瀏覽器, 使用本地設定檔中的選項配置瀏覽器"""
    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.啟動瀏覽器時自動載入外掛程式, 如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.啟動各種瀏覽器
    #startFirefox()
    #startFirefoxWithSpecificLocation()
    #startChrome()
    #startIE()  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.