本文整理汇总了Python中xpath.dsl.descendant函数的典型用法代码示例。如果您正苦于以下问题:Python descendant函数的具体用法?Python descendant怎么用?Python descendant使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了descendant函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: link
def link(locator):
"""
Returns an :class:`Expression` for finding links matching the given locator.
The query defines a link as an ``a`` element with an ``href`` attribute and will match links
that meet at least one of the following criteria:
* the element ``id`` exactly matches the locator
* the element ``title`` matches the locator
* the element text matches the locator
* the ``alt`` of a nested ``img`` element matches the locator
Args:
locator (str): A string that identifies the desired links.
Returns:
Expression: An :class:`Expression` object matching the desired links.
"""
expr = x.descendant("a")[x.attr("href")][
x.attr("id").equals(locator) |
x.attr("title").is_(locator) |
x.string.n.is_(locator) |
x.descendant("img")[x.attr("alt").is_(locator)]]
return expr
示例2: test_selects_a_nodes_text
def test_selects_a_nodes_text(self):
xpath = to_xpath(x.descendant("p")[x.text == "Bax"])
results = self.find_all(xpath)
self.assertEqual(results[0].text, "Bax")
self.assertEqual(results[1].get("title"), "monkey")
xpath = to_xpath(x.descendant("div")[x.descendant("p").text == "Bax"])
results = self.find_all(xpath)
self.assertEqual(results[0].get("title"), "fooDiv")
示例3: test_matches_encoded_unicode_characters
def test_matches_encoded_unicode_characters(self):
locator = "이름".encode("UTF-8") if sys.version_info >= (3, 0) else "이름"
xpath = to_xpath(x.descendant("div")[x.string.n.is_(locator)])
results = self.find_all(xpath)
self.assertEqual(results[0].get("id"), "unicode")
xpath = to_xpath(x.descendant("div")[x.attr("title") == locator])
results = self.find_all(xpath)
self.assertEqual(results[0].get("id"), "unicode")
示例4: test_matches_quotes
def test_matches_quotes(self):
locator = "Who's quotes? \"Their\" quotes."
xpath = to_xpath(x.descendant("div")[x.string.n.is_(locator)])
results = self.find_all(xpath)
self.assertEqual(results[0].get("id"), "quotes")
xpath = to_xpath(x.descendant("div")[x.attr("title") == locator])
results = self.find_all(xpath)
self.assertEqual(results[0].get("id"), "quotes")
示例5: test_second_order_union_aliased_as_plus_sign
def test_second_order_union_aliased_as_plus_sign(self):
expr1 = x.descendant("p")
expr2 = x.descendant("div")
expr3 = x.descendant("span")
collection1 = (expr1 + expr2)[x.attr("id") == "union-item-5"]
collection2 = collection1 + expr3[x.attr("id") == "union-item-6"]
xpath = to_xpath(collection2)
results = self.find_all(xpath)
self.assertEqual(results[0].text, "Fig")
示例6: test_creates_a_second_order_union
def test_creates_a_second_order_union(self):
expr1 = x.descendant("p")
expr2 = x.descendant("div")
expr3 = x.descendant("span")
collection1 = expr1.union(expr2)[x.attr("id") == "union-item-5"]
collection2 = collection1.union(expr3[x.attr("id") == "union-item-6"])
xpath = to_xpath(collection2)
results = self.find_all(xpath)
self.assertEqual(results[0].text, "Fig")
示例7: test_where_aliased_as_square_brackets
def test_where_aliased_as_square_brackets(self):
expr1 = x.descendant("p")
expr2 = x.descendant("div")
collection = expr1.union(expr2)
xpath1 = to_xpath(collection[x.attr("id").equals("union-item-3")])
xpath2 = to_xpath(collection[x.attr("id").equals("union-item-4")])
results = self.find_all(xpath1)
self.assertEqual(results[0].text, "Cherry")
results = self.find_all(xpath2)
self.assertEqual(results[0].text, "Date")
示例8: test_aliased_as_plus_sign
def test_aliased_as_plus_sign(self):
expr1 = x.descendant("p")
expr2 = x.descendant("div")
collection = expr1 + expr2
xpath1 = to_xpath(collection.where(x.attr("id").equals("union-item-3")))
xpath2 = to_xpath(collection.where(x.attr("id").equals("union-item-4")))
results = self.find_all(xpath1)
self.assertEqual(results[0].text, "Cherry")
results = self.find_all(xpath2)
self.assertEqual(results[0].text, "Date")
示例9: test_creates_a_union_expression
def test_creates_a_union_expression(self):
expr1 = x.descendant("p")
expr2 = x.descendant("div")
collection = expr1.union(expr2)
xpath1 = to_xpath(collection.where(x.attr("id").equals("union-item-3")))
xpath2 = to_xpath(collection.where(x.attr("id").equals("union-item-4")))
results = self.find_all(xpath1)
self.assertEqual(results[0].text, "Cherry")
results = self.find_all(xpath2)
self.assertEqual(results[0].text, "Date")
示例10: test_aliased_as_right_angle_bracket_equals
def test_aliased_as_right_angle_bracket_equals(self):
xpath = to_xpath(x.descendant("p")[x.position() >= 2])
results = self.find_all(xpath)
self.assertEqual(results[0].text, "Bax")
self.assertEqual(results[1].get("title"), "monkey")
self.assertEqual(results[2].text, "Bax")
self.assertEqual(results[3].text, "Blah")
示例11: test_finds_multiple_kinds_of_nodes_regardless_of_the_context
def test_finds_multiple_kinds_of_nodes_regardless_of_the_context(self):
xpath = to_xpath(x.descendant("div")[x.attr("id") == "woo"].anywhere("p", "ul"))
results = self.find_all(xpath)
self.assertEqual(inner_text(results[0]), "Blah")
self.assertEqual(inner_text(results[3]), "A list")
self.assertEqual(inner_text(results[4]), "A list")
self.assertEqual(inner_text(results[6]), "Bax")
示例12: test_used_as_an_expression
def test_used_as_an_expression(self):
parent = x.descendant("div")[x.attr("id").equals("union")]
expr1 = x.descendant("p")
expr2 = x.descendant("div")
collection = expr1.union(expr2)
xpath1 = to_xpath(parent.descendant(collection[x.attr("id").equals("union-item-3")]))
xpath2 = to_xpath(parent.descendant(collection[x.attr("id").equals("union-item-4")]))
results = self.find_all(xpath1)
self.assertEqual(results[0].text, "Cherry")
results = self.find_all(xpath2)
self.assertEqual(results[0].text, "Date")
示例13: test_checks_greater_than_or_equal
def test_checks_greater_than_or_equal(self):
xpath = to_xpath(x.descendant("p")[x.position().gte(2)])
results = self.find_all(xpath)
self.assertEqual(results[0].text, "Bax")
self.assertEqual(results[1].get("title"), "monkey")
self.assertEqual(results[2].text, "Bax")
self.assertEqual(results[3].text, "Blah")
示例14: field
def field(locator):
"""
Returns an :class:`Expression` for finding form fields matching the given locator.
The query defines a form field as one of the following:
* an ``input`` element whose ``type`` is neither "hidden", "image", nor "submit"
* a ``select`` element
* a ``textarea`` element
The query will match form fields that meet at least one of the following criteria:
* the element ``id`` exactly matches the locator
* the element ``name`` exactly matches the locator
* the element ``placeholder`` exactly matches the locator
* the element ``id`` exactly matches the ``for`` attribute of a corresponding ``label`` element
whose text matches the locator
* the element is nested within a ``label`` element whose text matches the locator
Args:
locator (str): A string that identifies the desired form fields.
Return:
Expression: An :class:`Expression` object matching the desired form fields.
"""
field_expr = x.descendant("input", "select", "textarea")[
~x.attr("type").one_of("hidden", "image", "submit")]
return _locate_field(field_expr, locator)
示例15: _locate_field
def _locate_field(field_expr, locator):
expr = field_expr[
x.attr("id").equals(locator) |
x.attr("name").equals(locator) |
x.attr("placeholder").equals(locator) |
x.attr("id").equals(x.anywhere("label")[x.string.n.is_(locator)].attr("for"))]
expr += x.descendant("label")[x.string.n.is_(locator)].descendant(field_expr)
return expr