selenium3 use Google headless browser, screenshots

Browser is the headless Headless Browser, no web browser graphical user interface, is usually controlled by the programming or command line interface.
During normal use of selenium, you have to worry about using the CPU and / or memory. Both approaches and graphics must be requested from the URL displayed in the browser is associated.
When using a headless browser, we do not have to worry about this. Therefore, we can expect the script we write memory consumption will be reduced, execution speed will be faster. When some people say, that I can not see the process in response to the browser, I can not stare at your own browser to perform my use case here. . . Oh give a bonus! Automation has always been to write efficient, screenshots you can do a thing, staring at? How about you do not hand the following points. . . Keke, connected text book

Download and install selenium, Google own browser downloads
a correspondence relationship Google Chrome and driven below

 

 

ChromeDriver Warehouse Address:
http://chromedriver.storage.googleapis.com/index.html

Google browser to view version way:

 

 

After downloading the driver has placed in the specified folder backup.

Scripting

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

#创建参数对象
chrome_options = Options()
#固定写法
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

#驱动路径
path = r'C:\Chromedriver\chromedriver.exe'

#创建浏览器对象
browser = webdriver.Chrome(executable_path=path,chrome_options=chrome_options)
#请求地址
url ='http://www.baidu.com/'
#发出请求
browser.get(url)
#休眠三秒
time.sleep(3)
#截图并保存在本地C盘TEST文件夹内
browser.save_screenshot("C:\\TEST\\baid2.png")
#截图保存在项目下
browser.save_screenshot('ph\\baid.png')

#其他截图方法
#browser.get_screenshot_as_base64()
#browser.get_screenshot_as_file()
#browser.get_screenshot_as_png()

#退出
browser.quit()

  执行后生成图片。

 

 

selenium4圣诞节已经发布,然后新功能API支持的更广泛,selenium的文档,官方自selenium2发布以来就从来没有更新过,哪怕是selenium3发布之后,selenium的官方也是不带动的,这次selenium的官方终于把selenium4的官方文档更新了。。。。。额,真香。

Guess you like

Origin www.cnblogs.com/beimingyouyuqingcangchuli/p/12167070.html