Столкнулся с проблемой: открываю сайт в 4 потока (multiprocess), что то делаю, после чего делаю скриншот, обрезаю и сохраняю, однако при скрытом браузере картинка пустая(не совсем пустая, просто белый фон в 1 байт), без headless работает всё как нужно, в инете инфы не нашёл, подскажите если кто сталкивался
from selenium import webdriver
import os, shutil
import urllib
import openpyxl
from PIL import Image
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.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from itertools import repeat
from multiprocessing import Pool
from multiprocessing import Manager, Process
import time
start_time = time.time()
WorkBook = openpyxl.load_workbook(r'C:\\MainBook-1.xlsm')
IntegralsSheet = WorkBook['Integrals']
CHROME_EXE_PATH = r'C:\\chromedriver.exe'
ArrayOfIntegrals = []
ArrayOfResults = []
for i in range(1, 18, 5):
Value = IntegralsSheet[f'A{i}'].value
Value = str(Value)
ArrayOfIntegrals.append(Value)
def get_data(Integral, ArrayOfResults):
try:
i = ArrayOfIntegrals.index(Integral)
driver = webdriver.Chrome(executable_path=CHROME_EXE_PATH)
driver.get('https://www.wolframalpha.com')
driver.execute_script("document.querySelectorAll('._2NL2 _TDWL _1kIy _1jze').forEach((element) => element.style.opacity = '0 !important')")
search = driver.find_element_by_xpath('//*[@id="__next"]/div/div[1]/div/div/div[1]/section/form/div/div/input')
button = driver.find_element_by_xpath('//*[@id="__next"]/div/div[1]/div/div/div[1]/section/form/span/button')
search.send_keys(Integral)
button.click()
driver.implicitly_wait(12)
result = driver.find_element_by_xpath('//*[@id="__next"]/div/div[1]/main/div[2]/div/div[2]/section/section[2]/div/div/img').get_attribute("alt")
get_scrin(i, driver)
except Exception as Ex:
pass
finally:
driver.close()
driver.quit()
def get_scrin(index, driver):
try:
cross = driver.find_element_by_xpath('//*[@id="__next"]/div/div[1]/main/div[1]/div[2]/ul/li[2]/div/div[3]/button')
cross.click()
except Exception:
pass
finally:
driver.save_screenshot( f'C:\\image{index+1}.png' )
time.sleep(.2)
im = Image.open(f'C:\\image{index+1}.png')
im_crop = im.crop((170,608,800,710))
im_crop.save(f'C:\\image{index+1}.png', quality=95)
if __name__ == '__main__':
folder = 'C:\\images'
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path): shutil.rmtree(file_path)
except Exception as e:
pass
for i in range(4):
my_file = open(f'C:\\image{i+1}.png', "w+")
my_file.close()
with Manager() as manager:
ArrayOfResults = manager.list()
p = Pool(processes=4)
p.starmap(get_data, zip(ArrayOfIntegrals, repeat(ArrayOfResults)))