欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

selenium 优化 提升性能

程序员文章站 2023-11-20 09:49:46
结果: 用时:7.200437545776367s用时:5.909301519393921s headless用时:4.924464702606201s headless\phone用时:4.935844421386719s headless\phone\no image 为了测试图片,http:/ ......

结果:

用时:7.200437545776367s
用时:5.909301519393921s headless
用时:4.924464702606201s headless\phone
用时:4.935844421386719s headless\phone\no image

为了测试图片,http://image.baidu.com/

 事实证明,no image对打开网页没影响,对网络有影响吧,这个有点

 1 from selenium import webdriver
 2 import time
 3 
 4 
 5 def method0():
 6     s1 = time.time()
 7     options = webdriver.chromeoptions()
 8     options.binary_location = r'd:\downloads\app\chrome\chrome.exe'
 9     browser = webdriver.chrome(options=options)
10     browser.get('http://image.baidu.com/')
11     browser.save_screenshot('baidu.png')
12     browser.quit()
13     s2 = time.time()
14     print('用时:{0}s'.format(s2 - s1))
15 
16 
17 def method1():
18     # 优化1 headless
19     s1 = time.time()
20     options = webdriver.chromeoptions()
21     options.binary_location = r'd:\downloads\app\chrome\chrome.exe'
22     options.headless = true
23     browser = webdriver.chrome(options=options)
24     browser.get('http://image.baidu.com/')
25     browser.save_screenshot('baidu_headless.png')
26     browser.quit()
27     s2 = time.time()
28     print('用时:{0}s headless'.format(s2 - s1))
29 
30 
31 def method2():
32     # 优化2 headless\phone
33     s1 = time.time()
34     options = webdriver.chromeoptions()
35     options.add_argument('user-agent={0}'.format('mqqbrowser/26 mozilla/5.0 (linux; u; android 2.3.7; zh-cn; mb200 build/grj22; cyanogenmod-7) applewebkit/533.1 (khtml, like gecko) version/4.0 mobile safari/533.1'))
36     options.binary_location = r'd:\downloads\app\chrome\chrome.exe'
37     options.headless = true
38     browser = webdriver.chrome(options=options)
39     browser.get('http://image.baidu.com/')
40     browser.save_screenshot('baidu_headless_phone.png')
41     browser.quit()
42     s2 = time.time()
43     print(r'用时:{0}s headless\phone'.format(s2 - s1))
44 
45 
46 def method3():
47     # 优化3 headless\phone\no image
48     s1 = time.time()
49     options = webdriver.chromeoptions()
50     options.add_argument('user-agent={0}'.format('mqqbrowser/26 mozilla/5.0 (linux; u; android 2.3.7; zh-cn; mb200 build/grj22; cyanogenmod-7) applewebkit/533.1 (khtml, like gecko) version/4.0 mobile safari/533.1'))
51     options.binary_location = r'd:\downloads\app\chrome\chrome.exe'
52     options.headless = true
53     options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
54     browser = webdriver.chrome(options=options)
55     browser.get('http://image.baidu.com/')
56     browser.save_screenshot('baidu_headless_phone_image.png')
57     browser.quit()
58     s2 = time.time()
59     print(r'用时:{0}s headless\phone\no image'.format(s2 - s1))
60 
61 
62 if __name__ == '__main__':
63     method0()
64     method1()
65     method2()
66     method3()