从循环中返回拆分列表"而" Python,Selenium

时间:2017-07-29 17:58:25

标签: python list selenium while-loop split

在我的应用程序中,我有一个用户表,但该表可以有超过1页的用户。我想用Selenium Webdriver Python获取所有页面中所有用户的列表。我有测试功能,它转到第一页,获取所有用户的列表,然后转到第二页获取用户列表,直到页面不再存在:

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)

我需要从我的循环

中的所有页面返回拆分列表

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

尝试使用list extend方法

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)

答案 2 :(得分:0)

$ cat $WEKAINSTALL/data/iris.arff |tail
6.8,3.2,5.9,2.3,Iris-virginica
6.7,3.3,5.7,2.5,Iris-virginica
6.7,3.0,5.2,2.3,Iris-virginica
6.3,2.5,5.0,1.9,Iris-virginica
6.5,3.0,5.2,2.0,Iris-virginica
6.2,3.4,5.4,2.3,Iris-virginica
5.9,3.0,5.1,1.8,Iris-virginica
%
%
%
$ java weka.filters.unsupervised.attribute.Discretize -i $WEKAINSTALL/data/iris.arff |tail
'\'(6.46-6.82]\'','\'(2.96-3.2]\'','\'(5.13-5.72]\'','\'(2.26-inf)\'',Iris-virginica
'\'(6.82-7.18]\'','\'(2.96-3.2]\'','\'(4.54-5.13]\'','\'(2.26-inf)\'',Iris-virginica
'\'(5.74-6.1]\'','\'(2.48-2.72]\'','\'(4.54-5.13]\'','\'(1.78-2.02]\'',Iris-virginica
'\'(6.46-6.82]\'','\'(2.96-3.2]\'','\'(5.72-6.31]\'','\'(2.26-inf)\'',Iris-virginica
'\'(6.46-6.82]\'','\'(3.2-3.44]\'','\'(5.13-5.72]\'','\'(2.26-inf)\'',Iris-virginica
'\'(6.46-6.82]\'','\'(2.96-3.2]\'','\'(5.13-5.72]\'','\'(2.26-inf)\'',Iris-virginica
'\'(6.1-6.46]\'','\'(2.48-2.72]\'','\'(4.54-5.13]\'','\'(1.78-2.02]\'',Iris-virginica
'\'(6.46-6.82]\'','\'(2.96-3.2]\'','\'(5.13-5.72]\'','\'(1.78-2.02]\'',Iris-virginica
'\'(6.1-6.46]\'','\'(3.2-3.44]\'','\'(5.13-5.72]\'','\'(2.26-inf)\'',Iris-virginica
'\'(5.74-6.1]\'','\'(2.96-3.2]\'','\'(4.54-5.13]\'','\'(1.78-2.02]\'',Iris-virginica
$