AJAX-loaded lists can present challenges for visibility verification in Selenium. To reliably check if a list containing multiple elements has fully loaded, consider the following approaches:
Using WebDriverWait, you can induce the visibility_of_all_elements_located() condition:
<code class="python">elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "ul.ltr li[id^='t_b_'] > a[id^='t_a_'][href]")))</code>
Alternatively, you can use XPath:
<code class="python">elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//ul[@class='ltr']//li[starts-with(@id, 't_b_')]/a[starts-with(@id, 't_a_') and starts-with(., 'Category')]")))</code>
To wait for a specific number of elements, such as 10, use a lambda function:
<code class="python">myLength = 9 WebDriverWait(driver, 20).until(lambda driver: len(driver.find_elements_by_xpath("//ul[@class='ltr']//li[starts-with(@id, 't_b_')]/a[starts-with(@id, 't_a_') and starts-with(., 'Category')]")) > int(myLength))</code>
<code class="python">myLength = 10 WebDriverWait(driver, 20).until(lambda driver: len(driver.find_elements_by_xpath("//ul[@class='ltr']//li[starts-with(@id, 't_b_')]/a[starts-with(@id, 't_a_') and starts-with(., 'Category')]")) == int(myLength))</code>
The above is the detailed content of How to Effectively Wait for Multiple Elements to Load in Selenium with Python?. For more information, please follow other related articles on the PHP Chinese website!