In order to manage ReCAPTCHA, I've tried following exception:
try:
#some code
except:
print('recaptcha')
time.sleep(5)
driver.close()
#Verify that command has been executed
print('Closed')
#restart webdriver
driver = webdriver.Chrome(service=s, options = options)
#recursive call of function
return parser(link, driver = driver)
Also tried with implicitly.wait
instead time.sleep
. However, drive.close
command falling with InvalidSessionIdException
.
try:
#some code
except InvalidSessionIdException
print(e.message)
The above solution proposed in selenium.common.exceptions.WebDriverException: Message: invalid session id using Selenium with ChromeDriver and Chrome through Python doesn't work for me and coming to infinite loop of exceptions. Other solutions from that question have the same logic that my code. Here is the traceback.
File E:\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:551, in WebDriver.close(self)
543 def close(self) -> None:
544 """Closes the current window.
545
546 :Usage:
(...)
549 driver.close()
550 """
--> 551 self.execute(Command.CLOSE)
File E:\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:440, in WebDriver.execute(self, driver_command, params)
438 response = self.command_executor.execute(driver_command, params)
439 if response:
--> 440 self.error_handler.check_response(response)
441 response["value"] = self._unwrap_value(response.get("value", None))
442 return response
File E:\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py:245, in ErrorHandler.check_response(self, response)
243 alert_text = value["alert"].get("text")
244 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here
--> 245 raise exception_class(message, screen, stacktrace)
InvalidSessionIdException: Message: invalid session id
I could close driver with driver.quit()
and then launch again, but in this case the process fails with the same error on the driver.get(url)
step.