From e6aa173fd077f410775c579699ae5cf1990dcba4 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Wed, 27 Jul 2022 17:30:05 +0200 Subject: [PATCH] fixed problems with screenshots #2 --- Main.py | 32 +++++++++++++++++--------------- Screenshot_test.py | 27 ++++++++++++++------------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Main.py b/Main.py index 4c97cff..1c2598a 100644 --- a/Main.py +++ b/Main.py @@ -43,7 +43,7 @@ def search_adresses(adress_list, filename_adresslist, driver): for i in range(len(adress_list)): line = adress_list[i] adress = line.split(file_split_char) - if((line != adress_list[0]) & (adress[columnIndexes[4]] == "") & (len((adress[columnIndexes[0]]) > 0)) & (len(adress[columnIndexes[1]] > 0)) & (len(adress[columnIndexes[2]] > 0)) & (len(adress[columnIndexes[3]] > 0))): + if((line != adress_list[0]) & (adress[columnIndexes[4]] == "") & (len(adress[columnIndexes[0]]) > 0) & (len(adress[columnIndexes[1]]) > 0) & (len(adress[columnIndexes[2]]) > 0) & (len(adress[columnIndexes[3]]) > 0)): print(adress) search_string = adress[columnIndexes[0]] + " " + adress[columnIndexes[1]] + " " + adress[columnIndexes[2]] + " " + adress[columnIndexes[3]] search_bar = driver.find_element(By.ID, "searchTypeahead1") @@ -85,7 +85,8 @@ def search_adresses(adress_list, filename_adresslist, driver): qr.make(fit=True) qr.make_image(fill='black', back_color='white').save(image_folder_qrcode + image_filename_qrcode) - # Create Screenshot + # Create Screenshot of Map + actions = ActionChains(driver) try: driver.execute_script("""var l = document.getElementsByClassName("ol-zoom ol-unselectable ol-control")[0]; l.parentNode.removeChild(l);""") @@ -97,29 +98,29 @@ def search_adresses(adress_list, filename_adresslist, driver): location = mapElement.location size = mapElement.size x = location["x"] - y = 43 # location["y"] + y = location["y"] w = x + size["width"] - h = y + size["height"] - area = (x, y, w, h) + h = size["height"] + area = (x, 0, w, h) + driver.execute_script("window.scrollTo(0, " + str(y) + ")") time.sleep(1) driver.save_screenshot(image_folder_map + image_filename_map) time.sleep(0.2) Image.open(image_folder_map + image_filename_map).crop(area).save( image_folder_map + image_filename_map) - # take Screenshot 2 + # Create Screenshot of Chart try: - chartElement = driver.find_element(By.ID, "chart") - actions = ActionChains(driver) - actions.move_to_element(chartElement).perform() + chartElement = driver.find_elements(By.XPATH, "//div[@id='chart']//*")[0] location = chartElement.location size = chartElement.size - x = 920 - y = 1000 - w = 1600 - h = 1300 - area = (x, y, w, h) - print("X: " + str(x) + "Y: " + str(y) + "W: " + str(w) + "H: " + str(h)) + x = location["x"] + y = location["y"] + w = x + size["width"] + h = size["height"] + area = (x, 0, w, h) + driver.execute_script("window.scrollTo(0, " + str(y) + ")") + print("X: " + str(x) + " Y: " + str(y) + " W: " + str(w) + " H: " + str(h)) time.sleep(1) driver.save_screenshot(image_folder_production + image_filename_production) @@ -171,6 +172,7 @@ def search_adresses(adress_list, filename_adresslist, driver): print("closing Thread") break + print("closing Thread") def read_adresslist(filename_adresslist): returnvalues = [] diff --git a/Screenshot_test.py b/Screenshot_test.py index 5e75522..aac0173 100644 --- a/Screenshot_test.py +++ b/Screenshot_test.py @@ -19,7 +19,7 @@ driver = webdriver.Chrome(service=s) driver.minimize_window() driver.maximize_window() driver.get("https://www.uvek-gis.admin.ch/BFE/sonnendach/") -driver.implicitly_wait(20) +driver.implicitly_wait(5) try: if(not os.path.exists("screenshots")): @@ -81,6 +81,7 @@ try: "qrcodes/" + image_filename + "production" + ".png") # Create Screenshot + actions = ActionChains(driver) try: driver.execute_script("""var l = document.getElementsByClassName("ol-zoom ol-unselectable ol-control")[0]; l.parentNode.removeChild(l);""") @@ -92,27 +93,27 @@ try: location = mapElement.location size = mapElement.size x = location["x"] - y = 43 # location["y"] + y = location["y"] w = x + size["width"] - h = y + size["height"] - area = (x, y, w, h) + h = size["height"] + area = (x, 0, w, h) + driver.execute_script("window.scrollTo(0, " + str(y) + ")") time.sleep(1) driver.save_screenshot("screenshots/" + image_filename + "map" + ".png") time.sleep(0.2) Image.open("screenshots/" + image_filename + "map" + ".png").crop(area).save("screenshots/" + image_filename + "map" + ".png") #take Screenshot 2 - chartElement = driver.find_element(By.ID, "chart") - actions = ActionChains(driver) - actions.move_to_element(chartElement).perform() + chartElement = driver.find_elements(By.XPATH, "//div[@id='chart']//*")[0] location = chartElement.location size = chartElement.size - x = 920 - y = 1000 - w = 1600 - h = 1300 - area = (x, y, w, h) - print("X: " + str(x) + "Y: "+ str(y) + "W: "+ str(w) + "H: " + str(h)) + x = location["x"] + y = location["y"] + w = x + size["width"] + h = size["height"] + area = (x, 0, w, h) + driver.execute_script("window.scrollTo(0, " + str(y) + ")") + print("X: " + str(x) + " Y: "+ str(y) + " W: "+ str(w) + " H: " + str(h)) time.sleep(1) driver.save_screenshot("screenshots/" + image_filename + "production" + ".png")