Alpha's Arena
Alpha's Arena

Reputation: 1

Selenium from Chrome webdriver

What this code is supposed to do is open the webpage https://op.gg/champion/Sett/statistics/Top and get screenshot a part of the page.
The code currently only pulls up https://na.op.gg/champion/statistics. I believe this is why It wont screen shot the section intended, I do not know how to fix it.

from selenium import webdriver
from PIL import Image
import os

def get_runes(champ, lane, dark_mode):
    google_chrome_options = webdriver.chrome.options.Options()
    google_chrome_options.headless = True
    google_chrome_options.add_argument('--window-size=568,300')
    web_driver = webdriver.Chrome(
        os.getcwd() + "\chromedrive",
        options=google_chrome_options
    )
    url = f"https://op.gg/champion/Sett/statistics/Top"
    web_driver.get(url)
    element = web_driver.find_element_by_class_name('perk-page-wrap')
    element = element.screenshot_as_png
    web_driver.close()

    img = Image.open(io.BytesIO(element))
    if dark_mode is True:
        img = img.convert("RGBA")
        data = img.getdata()

        new_data = []
        for item in data:
            if item[0] == 245 and item[1] == 245 and item[2] == 245:
                new_data.append((0, 0, 255, 0))
            else:
                new_data.append(item)
        
        img.putdata(new_data)
    
    return img

Upvotes: 0

Views: 63

Answers (1)

Prophet
Prophet

Reputation: 33361

  1. try setting the screen to normal size. Instead of
google_chrome_options.add_argument('--window-size=568,300')

put

google_chrome_options.add_argument('--window-size=1920,1080')
  1. There are 4 elements matching find_element_by_class_name('perk-page-wrap') while you are making a screenshot for the first one only.
    This will do the same for first 2 elements (the other 2 are not matching visible elements)
screenshots = []
elements = web_driver.find_elements_by_class_name('perk-page-wrap')
    for i in range(2)
    screenshot = element[i].screenshot_as_png
    screenshots.append(screenshot)

Upvotes: 1

Related Questions