Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium:检索向下滚动时加载的数据_Python_Ajax_Testing_Selenium_Integration Testing - Fatal编程技术网

Python Selenium:检索向下滚动时加载的数据

Python Selenium:检索向下滚动时加载的数据,python,ajax,testing,selenium,integration-testing,Python,Ajax,Testing,Selenium,Integration Testing,我试图检索一个页面中的元素,该页面具有ajax加载向下滚动功能alla Twitter。由于某些原因,这不能正常工作。我添加了一些print语句来调试它,我总是得到相同数量的项,然后函数返回。我做错了什么 wd = webdriver.Firefox() wd.implicitly_wait(3) def get_items(items): print len(items) wd.execute_script("window.scrollTo(0, document.body.

我试图检索一个页面中的元素,该页面具有ajax加载向下滚动功能alla Twitter。由于某些原因,这不能正常工作。我添加了一些print语句来调试它,我总是得到相同数量的项,然后函数返回。我做错了什么

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'))

注意:睡眠困难只是为了证明它是有效的。请使用waits包来等待智能条件。

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

```

你说工作不正常是什么意思?有错误消息吗?没有,它只是在我执行scrolldown时没有找到加载的数据,因此while循环中的条件总是错误的。页面是否确实滚动到底?你看到新的推文加载了吗?