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?