简体   繁体   English

从“ while”循环中返回拆分列表,Python,Selenium

[英]Return splitted list from loop “while” Python, Selenium

In my application, I have a table with users but the table can have more than 1 page with users. 在我的应用程序中,我有一个包含用户的表,但是该表可以包含多个用户页面。 I want to get a list with all users in all pages with Selenium Webdriver Python. 我想使用Selenium Webdriver Python获取所有页面中所有用户的列表。 I have test function which goes to the first page, gets a list with all users then goes to the second page gets a list of users and until the pages no longer exist: 我有测试功能,该功能转到第一页,获取所有用户的列表,然后转到第二页,获取用户列表,直到页面不再存在:

def test_users1(driver):
    login(driver, username="Admin", password="Password")
    while True:
         try:
            #gets list of elements
            userslist = driver.find_elements_by_css_selector(".even .odd")
            #goes to second page, third...etc.
            for i in range(1, 50):
                driver.find_element_by_link_text("%s" % i).click()
         except NoSuchElementException:
            break
         return (userslist)

I need to return split list with all users from all pages from my loop 我需要返回循环中所有页面的所有用户的拆分列表

def users1(driver):
userslist = []
while True:
    try:
        #gets list of elements
        userslist.extend(driver.find_elements_by_css_selector("tbody tr"))
        #goes to second page, third...etc.
        for i in range(1, 50):
            driver.find_element_by_link_text("%s" % i).click()
    except NoSuchElementException:
        break
return len(userslist)

def test_users1(driver):
login(driver, username="Admin", password="Password")
assert users1(driver) == 50

Try use list extend method 尝试使用列表扩展方法

def test_users1(driver):
    login(driver, username="Admin", password="Password")
    userslist = []
    while True:
        try:
            #gets list of elements
            userslist.extend(driver.find_elements_by_css_selector(".even .odd"))
            #goes to second page, third...etc.
            for i in range(1, 50):
                driver.find_element_by_link_text("%s" % i).click()
                userslist.extend(driver.find_elements_by_css_selector(".even .odd"))
                # Add new line here ^^^^
        except NoSuchElementException:
            break
    return (userslist)
def users1(driver):
    userslist = []
    while True:
        try:
            #gets list of elements
            userslist.extend(driver.find_elements_by_css_selector(".even .odd"))
            #goes to second page, third...etc.
            for i in range(1, 50):
                driver.find_element_by_link_text("%s" % i).click()
        except NoSuchElementException:
            break
    return userslist


def test_users1(driver):
    login(driver, username="Admin", password="Password")
    users = users1(driver)
    assert len(users) == 63

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM