Issue
I got this code mostly working except that the images saved are not really adequate. My x, y, width, height take screenshots that are not matching the images. I think there is a better way to do this but I don't know how. Any help appreciated.
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome import options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from PIL import Image
import time
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--start-maximized')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://superrare.com/features/rare-steals-1")
time.sleep(2)
imgsrc2 = WebDriverWait(driver,50).until(EC.presence_of_all_elements_located((By.XPATH, "//*[contains(@class,'slide selected')]")))
#the element with longest height on page
ele=driver.find_element("xpath", '//div[@id="root"]')
total_height = ele.size["height"]+8000
driver.set_window_size(1920, total_height) #the trick
time.sleep(2)
driver.save_screenshot("screenshot1.png")
for i in imgsrc2:
location = i.location
size = i.size
x = location['x']
y = location['y']
width = x + size['width']
height = y + size['height']
fullImg = Image.open("screenshot1.png")
cropImg = fullImg.crop((x, y, x + width, y + height))
cropImg.save("\\images\\new-img" + str(i) + ".png")
driver.quit()
The large screenshot of the whole page is fine but I get cropped images like this one
Solution
Here is the code using the firefox driver:
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome import options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from PIL import Image
import time
driver = webdriver.Firefox()
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get("https://superrare.com/features/the-intersection-of-machine-and-artist")
time.sleep(2)
#the element with longest height on page
ele=driver.find_element("xpath", '//div[@id="root"]')
total_height = ele.size["height"]+12000
time.sleep(2)
driver.set_window_size(2500, total_height) #the trick
time.sleep(2)
driver.save_screenshot("screenshot1.png")
time.sleep(3)
imgsrc = WebDriverWait(driver,50).until(EC.presence_of_all_elements_located((By.XPATH, "//*[contains(@class,'slide selected')]")))
imgsrc2 = WebDriverWait(driver,50).until(EC.presence_of_all_elements_located((By.XPATH, "//*[contains(@class,'slide selected')]")))
time.sleep(10)
for i in imgsrc2:
location = i.location
size = i.size
x = location['x']
y = location['y']
width = x + size['width']
height = y + size['height']
# print(x, y, width, height)
fullImg = Image.open("screenshot1.png")
# cropImg = fullImg.crop((x, y, x + width, y + height))
cropImg = fullImg.crop((int(x), int(y), int(width), int(height)))
cropImg.save("\\images\\new-img" + str(i) + ".png")
driver.close()
Answered By - Remi Castonguay
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.