InvalidSessionIdException:消息:无效的会话 ID 使用 Selenium 和 Python 在循环中截取屏幕截图

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

我正在 python 中用 selenium 编写一个程序来搜索网站(每次都会更改但格式相似)并从中下载图像。如果有版权,程序会关闭选项卡,否则它会运行程序的其余部分。 但是有一个问题:它抛出一个 InvalidSessionIdException。

下面是我写的代码和相对错误

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()

错误是这样的

  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

另一个问题是程序总是下载图像而不关心“版权”的存在

如何解决这些问题?

看起来对版权没有任何依赖。

save_screenshot() you can in the and use the following

  • 代码块:

    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()
    
  • 截图:

    • image_0

    • image_1