screenshot python_python 截图screenshot处理

 2023-09-05 阅读 86 评论 0

摘要:from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport timeimport osdef screenshot(driver,file_path = None):#用户没有传参数if file_path == None:project_path = os.path.dirname(os.getcwd())print(

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

finally:

time.sleep(3)

driver.quit()

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/785.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息