問題描述
我正在使用 Selenium 編寫一些代碼,有一次我向不同的網站發出 7 個請求.對于第一個,這很好用.但是,對于其他人,我收到會話 ID 錯誤.我認為我的瀏覽器配置正確,因為我確實從第一個網站獲得了結果.我試圖在請求之間放置一個 WebDriverWait,但無濟于事.我認為這些網站可能會阻止我的請求.有誰知道如何解決這個問題?
I'm writing some code using Selenium, and at one point I make 7 requests, all to different websites. For the first one, this works fine. However, for others, I get a session ID error. I think that my browser is configured correctly, as I do get results from the first website. I have tried to put a WebDriverWait in between the requests, but to no avail. I think the websites might be blocking my requests. Does anyone have any idea how to solve this problem?
如果這是愚蠢的事情或者我做錯了什么,我很抱歉,我是新手^^
I'm sorry if this is something stupid or if I'm doing anything wrong, I'm quite new ^^
提前致謝!
Traceback (most recent call last):
File "/home/cena/PycharmProjects/Frikandelbroodje/main.py", line 56, in <module>
dirk_price = get_price(dirk_url, dirk_classname)
File "/home/cena/PycharmProjects/Frikandelbroodje/main.py", line 44, in get_price
browser.get(url)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid session id
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.15.0-50-generic x86_64)
推薦答案
無效的會話 ID
無效的會話 ID 錯誤是 當服務器無法識別唯一會話標識符時發生的 WebDriver 錯誤.如果會話已被刪除或會話 ID 無效,則會發生這種情況.
invalid session id
The invalid session ID error is a WebDriver error that occurs when the server does not recognize the unique session identifier. This happens if the session has been deleted or if the session ID is invalid.
可以通過以下任一方式刪除 WebDriver 會話:
A WebDriver session can be deleted through either of the following ways:
顯式會話刪除:當顯式調用
quit()
方法時,WebDriver 會話被顯式刪除,如下所示:
Explicit session deletion: A WebDriver session is explicitly deleted when explicitly invoking the
quit()
method as follows:
代碼塊:
Code Block:
from selenium import webdriver
from selenium.common.exceptions import InvalidSessionIdException
driver = webdriver.Chrome(executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
print("Current session is {}".format(driver.session_id))
driver.quit()
try:
driver.get("https://www.google.com/")
except Exception as e:
print(e.message)
控制臺輸出:
Console Output:
Current session is a9272550-c4e5-450f-883d-553d337eed48
No active session with ID a9272550-c4e5-450f-883d-553d337eed48
隱式會話刪除:當您關閉最后一個窗口或選項卡調用 close()
方法時,會隱式刪除 WebDriver 會話,如下所示:
Implicit session deletion: A WebDriver session is implicitly deleted when you close the last window or tab invoking close()
method as follows:
代碼塊:
Code Block:
driver = webdriver.Chrome(executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
print("Current session is {}".format(driver.session_id))
# closes current window/tab
driver.close()
try:
driver.get("https://www.google.com/")
except Exception as e:
print(e.message)
控制臺輸出:
Console Output:
Current session is a9272550-c4e5-450f-883d-553d337eed48
No active session with ID a9272550-c4e5-450f-883d-553d337eed48
由于第一個請求工作正常,但對于其他請求,您會收到 會話 ID 錯誤,很可能是 WebDriver 控制的 Web 瀏覽器 正在被檢測到并因此阻止下一個請求.
As the first one request works fine but for others you get a session ID error most possibly the WebDriver controled Web Browser is getting detected and hence blocking the next requests.
WebDriver 控制的Web 瀏覽器 被檢測到并同時被阻止的原因有多種.您可以在以下位置找到一些詳細的討論:
There are different reasons for the WebDriver controled Web Browser to get detected and simultaneously get blocked. You can find a couple of detailed discussion in:
- recaptcha 3 怎么知道我?m 使用 selenium/chromedriver?
- Selenium 和非無頭瀏覽器保持要求驗證碼
這篇關于selenium.common.exceptions.WebDriverException:消息:通過 Python 使用 Selenium 與 ChromeDriver 和 Chrome 的會話 ID 無效的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!