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

你可能感兴趣的:(python使用selenium截图特定元素)