ID phiên không hợp lệ

Lỗi ID phiên không hợp lệ là một lỗi WebDriver xảy ra khi máy chủ không nhận ra mã định danh phiên duy nhất. Điều này xảy ra nếu phiên đã bị xóa hoặc nếu ID phiên không hợp lệ.

Ví dụ

Xóa phiên một cách tường minh

Một phiên WebDriver được xóa tường minh khi thoát:

python
from selenium import webdriver
from selenium.common import exceptions

session = webdriver.Firefox()
print("Current session is {}".format(session.session_id))
session.quit()

try:
    session.get("https://mozilla.org")
except exceptions.InvalidSessionIdException as e:
    print(e.message)

Kết quả:

Current session is 46197c16-8373-469b-bc56-4c4d9e4132b4
No active session with ID 46197c16-8373-469b-bc56-4c4d9e4132b4

Xóa phiên ngầm

Phiên cũng có thể bị xóa ngầm nếu bạn đóng cửa sổ hoặc tab cuối cùng:

python
from selenium import webdriver
from selenium.common import exceptions

session = webdriver.Firefox()
print("Current session is {}".format(session.session_id))

# đóng cửa sổ/tab hiện tại
session.close()

try:
    session.get("https://mozilla.org")
except exceptions.InvalidSessionIdException as e:
    print(e.message)

Kết quả:

Current session is 46197c16-8373-469b-bc56-4c4d9e4132b4
No active session with ID 46197c16-8373-469b-bc56-4c4d9e4132b4

Xem thêm