Selenium之鼠标悬浮,窗口切换,截图

Selenium之鼠标悬浮

import time
from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Chrome()
# driver.get('https://www.cnblogs.com/TodayWind/')
# 悬浮
# ActionChains(driver).move_to_element(driver.find_element_by_id('Header1_HeaderTitle')).perform()  # perform代表执行的意思

# 拖拽
driver.get("https://www.jq22.com/demo/pintu20151229/")
time.sleep(1)
driver.find_element_by_id('start').click()
time.sleep(1)
start = driver.find_element_by_xpath('//*[@id="container"]/div[3]')
end = driver.find_element_by_xpath('//*[@id="container"]/div[14]')
ActionChains(driver).drag_and_drop(start, end).perform()
# 鼠标左键单击
ActionChains(driver).click(driver.find_element_by_xpath('')).perform()
# 鼠标左键双击
ActionChains(driver).double_click(driver.find_element_by_xpath('')).perform()
# 鼠标右键单击
ActionChains(driver).context_click(driver.find_element_by_xpath('')).perform()

 窗口切换

from selenium import webdriver

driver = webdriver.Chrome()

driver.switch_to.window()  # 切换窗口

driver.switch_to.frame()  # 进入iframe弹框

driver.switch_to.default_content()  # 退出iframe

# window_handles  # 窗口数组

截图

from selenium import webdriver
from PIL import Image
import time

# 1. # 截全图(非完整图,屏幕100%的图)
a_img = './a.png'  # 只能是png格式
b_img = './b.png'
driver = webdriver.Chrome()
driver.implicitly_wait(30)
driver.get('http://www.baidu.com')
driver.save_screenshot(a_img)

# 2.只截取验证码的图片
# 获取验证码的大小
imageCode = driver.find_element_by_id('imageCode')  # 定位到图片
print(imageCode.size)  # 查看图片的大小
left = imageCode.location['x'] * 1.24  # * 1.24是因为浏览器缩放的问题
top = imageCode.location['y'] * 1.24
right = imageCode.size['width'] + left
height = imageCode.size['height'] + top
temp = Image.open(a_img)
temp = temp.crop((left, top, right, height))  # 这里是元组
temp.save(b_img)

# 只截取验证码的图片 使用selenium 取巧截取图片
imageCode = driver.find_element_by_id('imageCode')  # 定位到图片
imageCode.screenshot(a_img)

 

posted @ 2021-06-19 17:30  断浪狂刀忆年少  阅读(448)  评论(0编辑  收藏  举报