Python+selenium启动浏览器Firefox\Chrome\IE
在写一功能的时候,本来准备使用webbroswer模块实现,不过发现其只能实现简单的打开,而且打开tab页面发现并不能实现。所以决能通过selenium模块实现,这里列下几个selenium操作不同浏览器的定义的函数。
一、webbroswer模块
该模块的非常简单,如下:
1import webbrowser
2c = webbrowser.get('firefox')
3c.open_new('https://blog.361way.com')
4c.open_new_tab('https://blog.361way.com')
执行的时候,发现open_new和open_new_tab操作效果一样。
二、selenium操作常见浏览器函数
1# -*- coding:utf-8 -*-
2import os
3import selenium
4from selenium import webdriver
5from selenium.webdriver.common.keys import Keys
6"""
7练习启动各种浏览器:Firefox, Chrome, IE
8练习启动各种浏览器的同时加载插件:Firefox, Chrome, IE
9"""
10def startFirefox():
11 """启动安装在默认位置的Firefox浏览器,并自动转到 百度 首页"""
12 driver = webdriver.Firefox()
13 driver.get("http://www.baidu.com")
14 assert("百度" in driver.title)
15 elem = driver.find_element_by_name("wd")
16 elem.send_keys("selenium")
17 elem.send_keys(Keys.RETURN)
18 assert "百度" in driver.title
19 driver.close()
20 driver.quit()
21 driver = None
22def startFirefoxWithSpecificLocation():
23 """启动安装在 非 默认位置的Firefox浏览器,并自动转到 百度 首页"""
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("百度" in driver.title)
29 elem = driver.find_element_by_name("wd")
30 elem.send_keys("selenium")
31 elem.send_keys(Keys.RETURN)
32 assert "百度" in driver.title
33 driver.close()
34 driver.quit()
35 driver = None
36def startChrome():
37 """启动Chrome浏览器,并自动转到 百度 首页
38 启动Chrome浏览器需要指定驱动的位置
39 """
40 chrome_driver = os.path.abspath(r"D:\云盘\360云\360云盘\我的自动双向同步文件夹\01-PersonalInfo\DataGuru\12-软件自动化测试Selenium2\1-课程\练习代码_Python版本\Selenium_python\Files\chromedriver.exe")
41 os.environ["webdriver.chrome.driver"] = chrome_driver
42 driver = webdriver.Chrome(chrome_driver)
43 driver.get("http://www.baidu.com")
44 assert("百度" in driver.title)
45 elem = driver.find_element_by_name("wd")
46 elem.send_keys("selenium")
47 elem.send_keys(Keys.RETURN)
48 assert "百度" in driver.title
49 driver.close()
50 driver.quit()
51 driver = None
52def startIE():
53 """启动IE浏览器,并自动转到 百度 首页
54 启动 IE 浏览器需要指定驱动的位置
55 """
56 ie_driver = os.path.abspath(r"D:\云盘\360云\360云盘\我的自动双向同步文件夹\01-PersonalInfo\DataGuru\12-软件自动化测试Selenium2\1-课程\练习代码_Python版本\Selenium_python\Files\IEDriverServer.exe")
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 "百度" in driver.title
66 driver.close()
67 driver.quit()
68 driver = None
69 '''
70def start_firefox_with_firebug_plug():
71 """启动Firefox,并自动加载插件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")
82def start_chrome_with_chrometomobile_plug():
83 """启动Chrome,并自动加载插件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 '''
99def start_firefox_with_default_settings():
100 """启动Firefox浏览器, 使用本地配置文件中的选项配置浏览器
101 自动将页面载入过程导出为Har文件,并存放在
102 配置项 extensions.firebug.netexport.defaultLogDir指定的D:\temp\selenium2目录下
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 # 使用从别的机器上拷贝来的浏览器配置
107 firefox_profile = webdriver.FirefoxProfile(os.path.abspath(r"D:\Temp\selenium2\Profiles\mm9zxom8.default"))
108 # 使用本地的默认配置
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 '''
118def start_chrome_with_default_settings():
119 """启动Firefox浏览器, 使用本地配置文件中的选项配置浏览器"""
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")
131if __name__ == "__main__":
132 # 2.启动浏览器时自动加载插件, 如Firefox -> Firebug ; Chrome -> 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.启动各种浏览器
138 #startFirefox()
139 #startFirefoxWithSpecificLocation()
140 #startChrome()
141 #startIE()
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/selenium-firefox-chrome-ie/6921.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.