| 489 | |
| 490 | |
| 491 | def test_select(): |
| 492 | p = rl.Parser(rl.HtmlOptions()) |
| 493 | p.process( |
| 494 | """<!DOCTYPE html> |
| 495 | <html> |
| 496 | <head> |
| 497 | <meta charset="UTF-8"> |
| 498 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 499 | <title>Document</title> |
| 500 | </head> |
| 501 | <body> |
| 502 | <div id="header" data-role="banner"> |
| 503 | <h1 title="main heading">Welcome</h1> |
| 504 | <p>This is paragraph 1</p> |
| 505 | <p>This is paragraph 2</p> |
| 506 | </div> |
| 507 | <ul class="menu"> |
| 508 | <li class="example">Item 1</li> |
| 509 | <li>Item 2</li> |
| 510 | <li>Item 3</li> |
| 511 | </ul> |
| 512 | <a href="https://example.com" class="external">External Link</a> |
| 513 | <input type="text" required> |
| 514 | <input type="checkbox" checked> |
| 515 | <div class="box"></div> |
| 516 | <p lang="en">English</p> |
| 517 | <p lang="fr">Texte en</p> |
| 518 | </body> |
| 519 | </html>""" |
| 520 | ) |
| 521 | p.finish() |
| 522 | d = p.into_dom() |
| 523 | |
| 524 | is_ok = False |
| 525 | for node in rl.Select(d.root(), '[href*="example"]'): |
| 526 | assert node.name == "a" |
| 527 | |
| 528 | _, v = _get_attr(node.attrs, "href") |
| 529 | assert "example" in v |
| 530 | is_ok = True |
| 531 | |
| 532 | assert is_ok |
| 533 | |
| 534 | is_ok = False |
| 535 | for node in rl.Select(d.root(), "div p:nth-of-type(2)"): |
| 536 | assert node.name == "p" |
| 537 | |
| 538 | assert _get_text(node) == "This is paragraph 2" |
| 539 | is_ok = True |
| 540 | |
| 541 | assert is_ok |
| 542 | |
| 543 | is_ok = False |
| 544 | for node in rl.Select(d.root(), "div:empty"): |
| 545 | assert node.name == "div" |
| 546 | |
| 547 | assert node.class_list() == ["box"] |
| 548 | |