Your script might be accessing a UI component that is not available in the DOM. The cause could be network latency, as shown in the diagram below, or the DOM simply isn't ready to access.
You can avoid this issue by using either an explicit or implicit wait. The following section discusses the different types of waits and when to use each.
Explicit Versus Implicit
The WebDriver function
driver.implicitly_wait(n) configures WebDriver for the remainder of a session. If you try to access an element that is not available, WebDriver will retry for up to
n seconds to find that element in the DOM. This simple approach is often sufficient to make your scripts work reliably.
In the diagram below, the
WebDriver retries to click Button 2 until it's available.
An explicit wait is code that requires a certain condition to occur before continuing to execute code. Although you may be tempted to use
time.sleep() to wait for an element to be available, you should avoid doing this. Instead, use the WebDriver API methods given in 5.1. Explicit Waits of the Selenium Python Bindings documentation that enable you to wait for expected conditions.
In the diagram below, the explicit wait stipulates to click Button 2 (action) only when the button is available (the condition).