我试图找回在具有支持Ajax的负载向下滚屏功能阿拉的Twitter页面元素。 出于某种原因,这是不正常。 我加了一些打印语句来调试它,我总是得到相同的项金额,然后函数返回。 我在做什么错在这里?
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'))
尝试把睡眠之间
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'))
注:硬盘睡眠只是为了证明它的作品。 请使用等待包等待聪明的条件来代替。
在while循环的条件是我的使用情况的问题。 这是一个无限循环。 我用一个计数器固定的问题:
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
```