【发布时间】:2020-03-22 02:02:57
【问题描述】:
我想通过截取整个网站的屏幕截图并裁剪它来截取 Tableau Public 可视化的屏幕截图。我能够找到我的元素的高度和宽度,但是我的 x 和 y 坐标是错误的。我假设 x = 0 和 y = 0 是我的元素在 iframe 中的坐标,而不是网站中的坐标。
如何获取 iframe 的坐标以便正确裁剪图像?目前它只是裁剪网站的左侧部分。
from selenium import webdriver
import time
from PIL import Image
driver = webdriver.Chrome(executable_path=r'C:\Program Files\chromedriver.exe')
# Maximise window and zoom out
driver.get('chrome://settings/')
driver.maximize_window()
driver.execute_script('chrome.settingsPrivate.setDefaultZoom(0.6);')
driver.get("https://public.tableau.com/en-gb/gallery/super-pac-origins?tab=viz-of-the-day&type=viz-of-the-day")
time.sleep(5)
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
element = driver.find_element_by_xpath('//div[@id = "tab-dashboard-region"]')
location = element.location
print(location)
size = element.size
print(size)
# Take a screenshot
driver.save_screenshot("C:\\Desktop\\Images_png\\test1.png")
time.sleep(2)
#Crop image
x = location['x']
y = location['y']
width = location['x']+ size['width']
height = location['y']+ size['height']
im = Image.open("C:\\Desktop\\Images_png\\test1.png")
im = im.crop((int(x), int(y), int(width), int(height)))
im.save("C:\\Desktop\\Images_png\\Crop\\test1.png")
# Close the current tab
driver.close()
感谢您的帮助。
【问题讨论】: