python使用selenium截图特定元素

import time

from selenium import webdriver

from PIL import Image

# driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\Google\Chrome\Application\chromedriver\chromedriver.exe')

driver = webdriver.PhantomJS(executable_path=r"D:\python1\phantomjs-2.1.1-windows\phantomjs.exe")

# driver.set_window_size(935,895)

# driver.get('https://www.baidu.com/')

driver.get('http://dun.163.com/trial/jigsaw')

time.sleep(2)

driver.save_screenshot('bdbutton3.png')

# element = driver.find_element_by_id('su')

# element = driver.find_element_by_class_name("qrcode-img")

target = driver.find_element_by_xpath(r'//div[@class="tcapt_item is-right"]//img[@class="yidun_bg-img"]')

template = driver.find_element_by_xpath(r'//div[@class="tcapt_item is-right"]//img[@class="yidun_jigsaw"]')

print(template.location)                # 打印元素坐标

print(template.size)                    # 打印元素大小

left = template.location['x']

top = template.location['y']

right = template.location['x'] + template.size['width']

bottom = template.location['y'] + template.size['height']

im = Image.open('bdbutton3.png')

im = im.crop((left, top, right, bottom))

im.save('bdbutton4.png')

left = target.location['x']

top = target.location['y']

print(target.location)                # 打印元素坐标

print(target.size)                    # 打印元素大小

right = target.location['x'] + target.size['width']

bottom = target.location['y'] + target.size['height']

im = Image.open('bdbutton3.png')

im = im.crop((left, top, right, bottom))

im.save('bdbutton5.png')

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容