It’s quite often the case that my selenium script will be running, and then all of a sudden it will crash with an error:
<class 'selenium.common.exceptions.NoSuchElementException'>
Message: u'Unable to locate element: {"method":"id","selector":"the_element_id"}'
<traceback object at 0x1017a9638>
And if I run in interactive mode (python -i myseltest.py), if I simply do something like:
driver.switch_to_window(driver.window_handles[0])
And then run the specific find_element_by_id() again, it will succeed.
Is there any way to automatically attempt the driver.switch_to_window() invocation if an exception occurs?
<UPDATE>First, consider using implicit wait, because this problem occurs mostly when element’s presence is triggered by an on-page javascript, and you might get a few seconds long delay between DOMReady event and js functions or ajax queries execution.
</UPDATE>This?