'Python Screenshot as file with errors

I'm running python 3.10.4, I'm trying to take a screenshot and I received this error:

[15468:20292:0409/154343.065:ERROR:fallback_task_provider.cc(124)] Every renderer should have at least one task provided by a primary task provider. If a "Renderer" fallback task is shown, it is a bug. If you have repro steps, please file a new bug and tag it as a dependency of crbug.com/739782.
Traceback (most recent call last):
  File "C:\users\VDISUpport\desktop\delivery\arun6\delivery\FinalPython.py", line 82, in <module>
    driver.get_screenshot_as_file(NagiosCriticalScreenShot)
  File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1321, in get_screenshot_as_file
    png = self.get_screenshot_as_png()
  File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1357, in get_screenshot_as_png
    return b64decode(self.get_screenshot_as_base64().encode('ascii'))
  File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1369, in get_screenshot_as_base64
    return self.execute(Command.SCREENSHOT)['value']
  File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: target frame detached
  (Session info: MicrosoftEdge=100.0.1185.36)
Stacktrace:
Backtrace:
        Microsoft::Applications::Events::EventProperties::unpack [0x0058ABF3+57891]
        Microsoft::Applications::Events::ISemanticContext::SetCommonField [0x00467EA1+1418977]
        Microsoft::Applications::Events::ILogConfiguration::operator* [0x002542AA+3162]
        Microsoft::Applications::Events::DebugEventListener::~DebugEventListener [0x002473E9+85225]

Has anybody knows how to solve this?

(Code)

driver.get(NagiosWeb)

#gotoIframe side.php and move into the HostGroups/Summary
driver.switch_to.frame(driver.find_element(By.NAME,"side"))

#wait until SUMMARY becomes clickable
element = WebDriverWait(driver,7).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"body > div:nth-child(3) > div.navsectionlinks > ul > li:nth-child(5) > ul > li:nth-child(1) > a")))
element.click()


#Close that space and move to the other space called Main
driver.switch_to.default_content()

#gotoIframe main.php to go to critical and warning
driver.switch_to.frame(driver.find_element(By.NAME,"main"))


#click on the name of the group CORP-ADMIN 
element = WebDriverWait(driver,3).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'body > div > table > tbody > tr:nth-child(2) > td:nth-child(1) > a:nth-child(1)')))
element.click()

#click on the name of the group CRITICAL
element=WebDriverWait(driver,4).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'body > table > tbody > tr > td:nth-child(3) > table > tbody > tr:nth-child(1) > td > table > tbody > tr:nth-child(1) > th:nth-child(4) > a')))
element.click()

if not dir_path.is_dir():
    os.mkdir(dir_path)



######### CRITICAL ##########
time.sleep(3)

try:
    #take screenshot 
    #driver.get_screenshot_as_file(NagiosCriticalScreenShot)
    driver.save_screenshot(NagiosCriticalScreenShot)
    #pyautogui.screenshot(NagiosCriticalScreenShot,  region=(275,415, 1600, 600))
except FileNotFoundError:
    print("Unable to take the screenshot - critical")

element = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'body > table.headertable > tbody > tr > td:nth-child(3) > table > tbody > tr:nth-child(1) > td > table > tbody > tr:nth-child(1) > th:nth-child(2) > a')))
element.click()

######### WARNING  ##########
time.sleep(2)
try:
    #take screenshot 
    #driver.get_screenshot_as_file(NagiosWarnningScreenShot)
    driver.save_screenshot(NagiosWarnningScreenShot)
    #pyautogui.screenshot(NagiosWarnningScreenShot,  region=(275,415, 1600, 600))
except FileNotFoundError:
    print("Unable to take the screenshot - WARNING")

################################ ##
#login and open page - NAGIOS END #
################################ ##


driver.quit()

The issue is related to the code, don't know exactly what is the root cause and how to solve it.

The same code sometimes works sometimes does this error.

The web page is contained two main iframes.

one side where there are buttons and the ones called main where they are some information that is required on the screenshot.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source