zoukankan      html  css  js  c++  java
  • 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)
    幻想毫无价值,计划渺如尘埃,目标不可能达到。这一切的一切毫无意义——除非我们付诸行动。
  • 相关阅读:
    通过Form添加数据到数据库里
    如何取消服务器/主机空间目录脚本的执行权限
    一个人的网站开发
    3.1.2 新版视频教程震撼发布(30集)
    P类问题,NP,NPC,HPHard,coNP,NPI问题 的简单认识
    随机算法与近似算法
    python to exe
    ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)
    anaconda + pyqt5 + pycharm 安装,测试
    C++ 结构体初始化
  • 原文地址:https://www.cnblogs.com/TodayWind/p/14903631.html
Copyright © 2011-2022 走看看