目录

4. Finds

目录

webium.find.Finds 方法的用法跟Find方法基本是一致的,不过返回的是WebElement列表。

from selenium.webdriver.common.by import By
from webium import BasePage, Finds

class LinksPage(BasePage):
    links = Finds(by=By.TAG_NAME, value='a')

    def __init__(self):
        super(LinksPage, self).__init__(url='http://itest.info')


if __name__ == '__main__':
    page = LinksPage()
    page.open()
    print('Number of links: ' + str(len(page.links)))

我们甚至可以动态返回满足条件的一组元素。

from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from webium import BasePage, Finds


class Link(WebElement):
    def is_secure(self):
        return self.get_attribute('href').startswith('https://')


class TypedPage(BasePage):
    links = Finds(Link, By.TAG_NAME, 'a')

    def __init__(self):
        super(TypedPage, self).__init__(url='http://itest.info')

    def get_unsecured_links(self):
        return filter(lambda link: not link.is_secure(), self.links)


if __name__ == '__main__':
    page = TypedPage()
    page.open()
    print('Number of unsecured links: ' + str(len(page.get_unsecured_links())))

原始封面

https://images.unsplash.com/photo-1507208773393-40d9fc670acf?w=300