zoukankan      html  css  js  c++  java
  • python 截图screenshot处理

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    import time
    import os


    def screenshot(driver,file_path = None):
    #用户没有传参数
    if file_path == None:
    project_path = os.path.dirname(os.getcwd())
    print(project_path)
    file_path = project_path +"/images/"
    if not os.path.exists(file_path):
    os.mkdir(file_path)
    images_name = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    file_path = file_path+images_name+".png"
    print(file_path)
    driver.save_screenshot(file_path)

    try:
    driver = webdriver.Chrome()
    driver.maximize_window()
    url = "https://www.cnblogs.com/"
    driver.get(url)

    ele = driver.find_element_by_xpath('//*[@id="cate_item_2"]')

    ActionChains(driver).move_to_element(ele).perform()

    driver.find_element_by_partial_link_text('python').click()

    time.sleep(3)
    # driver.save_screenshot('jietu1.png')

    #浏览器句柄切换
    #先获取浏览器所有句柄
    handles = driver.window_handles
    #当前浏览器句柄
    current_handles = driver.current_window_handle
    #for循环句柄
    for handle in handles:
    #如果handle不是当前handle
    if handle != current_handles:
    #handle就句柄切换一下
    driver.switch_to.window(handle)
    screenshot(driver)#自己写的screenshot
    “”“
    #浏览器句柄切换
    #先获取浏览器所有句柄
    handles = driver.window_handles
    #当前浏览器句柄
    current_handles = driver.current_window_handle
    #for循环句柄
    for handle in handles:
    #如果handle不是当前handle
    if handle != current_handles:
    #handle就句柄切换一下
    driver.switch_to.window(handle)
    #screenshot(driver)#自己写的screenshot
    driver.save_screenshot("jietu22.png")
    ”“”


    finally:
    time.sleep(3)
    driver.quit()

  • 相关阅读:
    哀悼:设置我们的blog为银灰色
    五子棋算法详解——解决方案之二
    ActionScript3使用角度值控制游戏角色的动作和移动
    LR12中快照的一点使用
    转义字符的一些应用
    关联的一些设置及实践
    检查点函数实践
    LR12的log解释
    struts2的强大迭代标签:<s:iterator>
    正则表达式30分钟入门
  • 原文地址:https://www.cnblogs.com/wenjing2019/p/12192147.html
Copyright © 2011-2022 走看看