Sto cercando di recuperare elementi in una paginaha una funzionalità scroll-down con caricamento ajax su Twitter. Per qualche motivo questo non funziona correttamente, ho aggiunto alcune istruzioni di stampa per eseguire il debug e ottengo sempre la stessa quantità di elementi e quindi la funzione ritorna. Cosa sto facendo di sbagliato qui?
wd = webdriver.Firefox()
wd.implicitly_wait(3)
def get_items(items):
print len(items)
wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# len(items) and len(wd.find_elements-by...()) both always seem to return the same number
# if I were to start the loop with while True: it would work, but of course... never end
while len(wd.find_elements_by_class_name("stream-item")) > len(items):
items = wd.find_elements_by_class_name("stream-item")
print items
wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
return items
def test():
get_page("http://twitter.com/")
get_items(wd.find_elements_by_class_name("stream-item"))
risposte:
3 per risposta № 1Prova a mettere un sonno in mezzo
wd = webdriver.Firefox()
wd.implicitly_wait(3)
def get_items(items):
print len(items)
wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# len(items) and len(wd.find_elements-by...()) both always seem to return the same number
# if I were to start the loop with while True: it would work, but of course... never end
sleep(5) #seconds
while len(wd.find_elements_by_class_name("stream-item")) > len(items):
items = wd.find_elements_by_class_name("stream-item")
print items
wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
return items
def test():
get_page("http://twitter.com/")
get_items(wd.find_elements_by_class_name("stream-item"))
Nota: il duro sonno è solo per dimostrare che funziona. Si prega di utilizzare il pacchetto Waits per attendere invece una condizione intelligente.
0 per risposta № 2
La condizione nel ciclo while era il problema per il mio caso d'uso. Era un ciclo infinito. Ho risolto il problema utilizzando un contatore:
def get_items(items):
item_nb = [0, 1] # initializing a counter of number of items found in page
while(item_nb[-1] > item_nb[-2]): # exiting the loop when no more new items can be found in the page
items = wd.find_elements_by_class_name("stream-item")
time.sleep(5)
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
item_nb.append(len(items))
return items
`` `