Selenium+Chrome browser environment construction
Selenium's Python project official documentation: https://selenium-python.readthedocs.io/index.html
1. Introduction to Selenium
Selenium is a tool for automated testing of web applications. Selenium tests run directly in the browser, just like a real user is operating. Supported browsers include IE (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera, etc.
The main functions include: test compatibility with the browser-test your application to see if it can work well on different browsers and operating systems.
Test system functions-create regression tests to verify software functions and user requirements. Support automatic recording actions and automatic generation of test scripts in different languages such as .Net, Java, Perl, etc.
Selenium is also an open source framework that is also released under the Apache License 2.0 protocol.
1) Support platform
WebDriver supports browser testing on two mobile platforms, Android and BlackBerry. Android is currently the mobile platform with the largest market share. For automated testing on it, Appium is recommended. Appium extends the WebDriver protocol to support native applications, web applications, and hybrid applications on the ios platform and the Android platform.
2) Support browser
The browsers currently supported by WebDriver include: Firefox, Chrome, IE, Edge, Opera, Safari. Why did you choose the above browsers for support? Mainly related to the browser's kernel.
3) Support mode
HtmlUnit and PhantomJS are two special modes. We can regard them as pseudo-browsers. In this mode, they support the parsing of html**, **Java Saript, etc., but they will not actually render the page. Since CSS and GUI rendering are not performed, the operating efficiency is much faster than that of a real browser, and it is mainly used for functional testing.
2. Install the browser driver chromedriver under Windows
The browser driver corresponding to the Chrome version chromedriver.exe
:
chromedriver version | Supported Chrome version |
---|---|
v2.46 | v71-73 |
v2.45 | v70-72 |
v2.44 | v69-71 |
v2.43 | v69-71 |
v2.42 | v68-70 |
v2.41 | v67-69 |
v2.40 | v66-68 |
v2.39 | v66-68 |
v2.38 | v65-67 |
v2.37 | v64-66 |
v2.36 | v63-65 |
v2.35 | v62-64 |
v2.34 | v61-63 |
v2.33 | v60-62 |
v2.32 | v59-61 |
v2.31 | v58-60 |
v2.30 | v58-60 |
v2.29 | v56-58 |
v2.28 | v55-57 |
v2.27 | v54-56 |
v2.26 | v53-55 |
v2.25 | v53-55 |
v2.24 | v52-54 |
v2.23 | v51-53 |
v2.22 | v49-52 |
v2.21 | v46-50 |
v2.20 | v43-48 |
v2.19 | v43-47 |
v2.18 | v43-46 |
v2.17 | v42-43 |
v2.13 | v42-45 |
v2.15 | v40-43 |
v2.14 | v39-42 |
v2.13 | v38-41 |
v2.12 | v36-40 |
v2.11 | v36-40 |
v2.10 | v33-36 |
v2.9 | v31-34 |
v2.8 | v30-33 |
v2.7 | v30-33 |
v2.6 | v29-32 |
v2.5 | v29-32 |
v2.4 | v29-32 |
89.0.4389.23 | 89 |
88.0.4324.96 | 88 |
87.0.4280.88 | 87 |
86.0.4240.22 | 86 |
85.0.4183.87 | 85 |
84.0.4147.30 | 84 |
83.0.4103.39 | 83 |
81.0.4044.69 | 81 |
80.0.3987.106 | 80 |
79.0.3945.36 | 79 |
78.0.3904.105 | 78 |
77.0.3865.40 | 77 |
76.0.3809.126 | 76 |
75.0.3770.140 | 75 |
74.0.3729.6 | 74 |
73.0.3683.68 | 73 |
Take 360 speed browser as an example
1) View the Chrome version of the 360 speed browser: 86
image-20210304132019356.png
2) Download the corresponding version chromedriver.exe
: v86.0.4240.22
https://chromedriver.storage.googleapis.com/index.html
4)解压chromedriver_win32.zip
,将chromedriver.exe
移动到360极速浏览器主程序目录C:\Users\Caviar\AppData\Local\360Chrome\Chrome\Application
(这里也可以将chromedriver.exe
放置在某个固定目录,并将该目录添加到Windows的环境变量变量中去)
3. 安装Selenium库
使用pip安装,在CMD(指命令提示符)中执行命令:
pip install selenium
Selenium库安装成功:
4. 运行测试
test.py
:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
def main():
options = Options()
# options.binary_location指定360极速浏览器路径
options.binary_location = 'C:\\Users\\Caviar\\AppData\\Local\\360Chrome\\Chrome\\Application\\360chrome.exe'
# 创建Chrome驱动实例,executable_path指定chromedriver路径
driver = webdriver.Chrome(options=options, executable_path='C:\\Users\\Caviar\\AppData\\Local\\360Chrome\\Chrome\\Application\\chromedriver.exe')
driver.maximize_window() # 最大化浏览器
print('最大化浏览器')
driver.get("https://www.baidu.com/")
time.sleep(3)
driver.quit()
if __name__ == '__main__':
import traceback
try:
main()
except Exception:
traceback.print_exc()
#with open('error.txt', 'w', encoding='utf-8') as f:
# traceback.print_exc(file=f)
input('\n程序运行异常,按回车键退出...')