Python自动化测试系列[v1.0.1][浏览器静默模式启动]

在实际的自动化测试中,为了不让浏览器频繁起动关闭,可以采用静默模式执行,代码示例如下。

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
# 创建chrome的Option对象
chrome_options = Options()
# 添加静默参数
chrome_options.add_argument('--headless')
for i in range(100000):
    # 静默模式启动浏览器
    chrome_driver = webdriver.Chrome(options=chrome_options)
    # 打开页面 
    chrome_driver.get("http://www.yialife.co.za/contact.html")
    chrome_driver.maximize_window()
    chrome_driver.find_element_by_class_name("xhl-button-text").click()
    # 获取当前时间
    current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    chrome_driver.find_element_by_id("messageText").send_keys("audio notification testing at " + current_time)
    time.sleep(1)
    chrome_driver.find_element_by_id("sendBtn").click()
    print(current_time)
    time.sleep(5)
    # 删掉所有cookie
    chrome_driver.delete_all_cookies()
chrome_driver.quit()

发布了231 篇原创文章 · 获赞 188 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/dawei_yang000000/article/details/105648259