InvalidSessionIdException: Message: invalid session id taking screenshots in a loop using Selenium and Python
Asked Answered
B

1

3

I'm coding a program with selenium in python that search a website (that changes every time but the format is similar) and download the image from it. If there's the copyright the program close the tab, otherwise it runs the rest of the program. But there's a problem: it throws an InvalidSessionIdException.

Under this there's the code I wrote and the relative error

import time

import imageio as imageio
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC

PATH = Service("/Users/fscozano/documenti/chromedriver-2.exe")

print("setup")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

wait = WebDriverWait(driver, 10)

for i in range(5):
    driver.get("https://apod.nasa.gov/apod/random_apod.html")
    copyr = driver.find_elements(By.XPATH, "//center[.//b[contains(.,'Copyright')]]")
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'apod')))

    if copyr:
        driver.close()
    else:
        imageLink = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,
        "//h1[normalize-space()='Astronomy Picture of the Day']//following::p[2]//a/img"))).get_attribute("src")
        driver.get(imageLink)

        finalImage = driver.save_screenshot("/Users/fscozano/PycharmProjects/NFT_finalProject/images/" + "HightAltitudeImage" + str(i) + ".png")
        driver.close()

The error is this

  File "/Users/fscozano/PycharmProjects/NFT_finalProject/venv/main.py", line 19, in <module>
driver.get("https://apod.nasa.gov/apod/random_apod.html")
  File "/Users/fscozano/PycharmProjects/NFT_finalProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 436, in get
self.execute(Command.GET, {'url': url})
  File "/Users/fscozano/PycharmProjects/NFT_finalProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
  File "/Users/fscozano/PycharmProjects/NFT_finalProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id
Stacktrace:
0   chromedriver                        0x0000000101487269 __gxx_personality_v0 + 582729
1   chromedriver                        0x0000000101412c33 __gxx_personality_v0 + 106003
2   chromedriver                        0x0000000100fcfcdf chromedriver + 171231
3   chromedriver                        0x0000000100ff850e chromedriver + 337166
4   chromedriver                        0x0000000100ff9a95 chromedriver + 342677
5   chromedriver                        0x00000001014438ab __gxx_personality_v0 + 305803
6   chromedriver                        0x000000010145a863 __gxx_personality_v0 + 399939
7   chromedriver                        0x000000010145fc7f __gxx_personality_v0 + 421471
8   chromedriver                        0x000000010145bbba __gxx_personality_v0 + 404890
9   chromedriver                        0x0000000101437e51 __gxx_personality_v0 + 258097
10  chromedriver                        0x0000000101477158 __gxx_personality_v0 + 516920
11  chromedriver                        0x00000001014772e1 __gxx_personality_v0 + 517313
12  chromedriver                        0x000000010148e6f8 __gxx_personality_v0 + 612568
13  libsystem_pthread.dylib             0x00007ff800ccd514 _pthread_start + 125
14  libsystem_pthread.dylib             0x00007ff800cc902f thread_start + 15

Another problem is that the program always downloads the image and don't care about the presence of "copyright"

How can I solve these problems?

Blakely answered 24/12, 2021 at 15:34 Comment(0)
S
2

It doesn't looks like there is any dependency on the Copyright.

To save_screenshot() you can open the image url in the adjascent tab and use the following Locator Strategies:

  • Code Block:

    for i in range(2):
        driver.get("https://apod.nasa.gov/apod/random_apod.html")
        windows_before  = driver.current_window_handle
        WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://apod.nasa.gov/apod']")))
        imageLink = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1[normalize-space()='Astronomy Picture of the Day']//following::p[2]//a/img"))).get_attribute("src")
        driver.execute_script("window.open('" + imageLink +"');")
        windows_after = driver.window_handles
        new_window = [x for x in windows_after if x != windows_before][0]
        driver.switch_to.window(new_window)
        driver.save_screenshot(f"image_{str(i)}.png")
        driver.close()
        driver.switch_to.window(windows_before)
    driver.quit()
    
  • Screenshot:

    • image_0 image_0

    • image_1 image_1

Seduce answered 24/12, 2021 at 17:49 Comment(5)
How can I use this in a for loop? If I try it throws a MaxRetryError using this codeBlakely
You already had a range(5) to loop, right? What's wrong with that? MaxRetryError: Indicates connections are refused, your program is detected as a automated bot. Avoiding it would be a seperate story all together :)Seduce
So, how can I run that without throwing ma exception?Blakely
@FrancescoZanoncelli Not sure where you were stuck. Now I have incorporated the range() looping within the answer.Seduce
Idk what is changed but now it works. ThanksBlakely

© 2022 - 2024 — McMap. All rights reserved.