5. Containers
乙醇 创建于 about 7 years 之前
最后更新: less than a minute 之前
阅读数: 170
我们可以使用容器(containers)来限制定位范围,webium的containers特性可以让我们的定位代码更加干净容易理解,避免大规模的使用难懂晦涩的xpath。
考虑下面的页面结构,为了可以遍历每一个搜索结果,我们可以使用下面的代码
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from webium import Find, Finds, BasePage
class Vacancy(WebElement):
title = Find(by=By.CLASS_NAME, value='vacancy_title')
profession = Find(by=By.CLASS_NAME, value='vacancy_profession')
location = Find(by=By.CLASS_NAME, value='vacancy_location')
apply_button = Find(by=By.CLASS_NAME, value='vacancy_apply_button')
class VacanciesPage(BasePage):
vacancies_list = Finds(Vacancy, By.XPATH, '//div[@id="careers-vacancies"]/div[@data-id]')
def __init__(self):
super(VacanciesPage, self).__init__(url='http://wargaming.com/en/careers/vacancies/')
if __name__ == '__main__':
page = VacanciesPage()
page.open()
for vacancy in page.vacancies_list:
# search by CLASS_NAME='vacancy_title' is performed within one node
print('Title: ' + vacancy.title.text)