selenium wait for the page to load code example
Example 1: selenium java wait for page load
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(lastElementToLoad));
Example 2: wait until page loads selenium javascript
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Firefox()
driver.get("http://www.example.com") #This is a dummy website URl
try:
elem=WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.ID,"Element_to_be_found")) #This is a dummy element</span>
)
finally:
driver.quit()
Example 3: wait until page loads selenium javascript
<span class="kn">from selenium import webdriver
from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="k">import</span> <span class="n">By</span>
<span class="kn">from</span> <span class="nn">selenium.webdriver.support.ui</span> <span class="k">import</span> <span class="n">WebDriverWait</span>
<span class="kn">from</span> <span class="nn">selenium.webdriver.support</span> <span class="k">import</span> <span class="n">expected_conditions</span> <span class="k">as</span> <span class="n">EC</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Firefox</span><span class="p">()</span>
<span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"http://www.example.com"</span><span class="p">) #This is a dummy website URL</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">elem </span><span class="o">=</span> <span class="n">WebDriverWait</span><span class="p">(</span><span class="n">driver</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">until</span><span class="p">(</span>
<span class="n">EC</span><span class="o">.</span><span class="n">presence_of_element_located</span><span class="p">((</span><span class="n">By</span><span class="o">.</span><span class="n">ID</span><span class="p">,</span> <span class="s2">"Element_to_be_found"</span><span class="p">)) #This is a dummy element</span>
<span class="p">)</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">driver</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>