I wrote my own Discord Bot which is taking screenshot of a specific website. This scrip is very simple and thought it should work fast, but it isn't. I read a lot, and I think I'm not able to improve it. Today my driver.get(URL) not working fast- i think the website is a little broken. But yesterday I get a result of 10 seconds waiting to get one screenshot. Is there possibility to make it faster?
def taking_screenshot_clearoutsite(new_folder_path, folder, city_coordinates, driver, day=None):
(latitude, longitude), city_name = city_coordinates
url = f'https://clearoutside.com/forecast/{latitude}/{longitude}'
driver.get(url)
# driver.set_window_position(0, 0)
# driver.set_window_size(1500, 2000)
day_now = datetime.datetime.now().date().strftime("%d/%m/%Y")
day_now_obj = datetime.datetime.strptime(day_now, '%d/%m/%Y')
"""testing window size"""
# size = driver.get_window_size()
# print("Window size: width = {}px, height = {}px".format(size["width"], size["height"]))
try:
driver.find_element_by_xpath("//*[@aria-label='dismiss cookie message']").click()
# time.sleep(2)
except WebDriverException:
pass
if day:
if isinstance(day, int) and int(day) == datetime.datetime.now().day:
pass
elif isinstance(day, int) and int(day) != datetime.datetime.now().day:
searching_day_index = int(day) - day_now_obj.day
try:
driver.find_element_by_xpath \
(f"//div[@id='day_{searching_day_index}']//div[@class='fc_day_date'][contains(text(),"
f"'{day}')]").click()
except WebDriverException:
driver.find_element_by_xpath(f"// div[contains(text(),'{day}')]").click()
"""pause 1 second to let page loads"""
time.sleep(1)
"""save screenshot and return name of it to crop"""
screen_file_name = screenshot(driver, folder)
file = Image.open(screen_file_name)
top_add = searching_day_index * 100
bottom_add = searching_day_index * 100
final_photo = cropping_file(file, new_folder_path, CLEAROUTSITE_CROP_PARAM,
city_day=(city_name, int(day)), top_add=top_add, bottom_add=bottom_add)
os.remove(screen_file_name)
return final_photo
else:
try:
driver.find_element_by_xpath(f"//div[@id='day_{0}']//div[@class='fc_day_date'][contains(text(),"
f"'{datetime.datetime.now().day}')]").click()
except WebDriverException:
pass
"""pause 1 second to let page loads"""
time.sleep(1)
"""save screenshot and return name of it to crop"""
screen_file_name = screenshot(driver, folder)
file = Image.open(screen_file_name)
final_photo = cropping_file(file, new_folder_path, CLEAROUTSITE_CROP_PARAM, city_day=(city_name, day))
os.remove(screen_file_name)
return final_photo
driver = start_driver()
(city_coordinates), country = find_city_coordinates('elblag')
taking_screen_shot_clearoutsite('Z:\Python\Rozne_programy\Discord\saving_to_disc_task\clear\elblag',
'Z:\Python\Rozne_programy\Discord\saving_to_disc_task',
city_coordinates, driver, 21)
driver.get(url)depends on the speed of your internet connection and the status of the server, so, it's really out of your hand... \$\endgroup\$