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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM