Brinley
Brinley

Reputation: 721

Python selenium won't wait for a list of divs to load

I'm trying to use selenium to parse the following html page:

<html>
    <button>Press me</button>
    <body>
        <div class="user-list ng-scope"></div>
    </body>
</html>

When I press the button on the page the div is filled with a series of "loading" divs:

<html>
    <button>Press me</button>
    <body>
        <div class="user-list ng-scope">
            <div class="panel">
                 <div class="panel-body">
                     <i class="glyphicon.glyphicon-cog.spinner.ng- 
                      scope::before"></i>
                 </div>
            </div>
            <div class="panel">
                 <div class="panel-body">
                     <i class="glyphicon.glyphicon-cog.spinner.ng- 
                      scope::before"></i>
                 </div>
            </div>
        </div>
    </body>
</html>

Once the <div class="panel"></div>'s have loaded, the html looks like this:

<html>
    <button>Press me</button>
    <body>
        <div class="user-list ng-scope">
            <div class="panel">
                 <div class="panel-body">
                     <i class="glyphicon glyphicon-chevron-right"></i>
                 </div>
            </div>
            <div class="panel">
                 <div class="panel-body">
                     <i class="glyphicon glyphicon-chevron-right"></i>
                 </div>
            </div>
        </div>
    </body>
</html>

Notice the <i></i> class has changed. Basically I want to use a method in Python's selenium module to wait for the <div class="panel"></div>'s to load. My first thought was to wait for the <i></i> classes to change from glyphicon.glyphicon-cog.spinner.ng-scope::before to glyphicon-chevron-right which signals that the divs have loaded. I use the following line of python code to do this:

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver.wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "glyphicon-chevron-right")))

Unfortunately, this does not seem to work, as the program terminates before the divs have loaded completely. Does anyone know of a way to leverage Python's selenium to wait for an arbitrary number of elements to load?

Upvotes: 0

Views: 594

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193108

Seems you were almost there. As per the HTML you have shared to wait for the particular <div> element to be visible you can use the following solution:

WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element_by_xpath("//div[@class='user-list ng-scope']//i[@class='glyphicon glyphicon-chevron-right']")))

Upvotes: 0

raviraja
raviraja

Reputation: 706

you should give time period to wait.

WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "glyphicon-chevron-right")))

Upvotes: 1

Related Questions