def test_iselect(self): """Test select iterator.""" markup = """ <!-- before header --> <html> <head> </head> <body> <!-- comment --> <p id="1"><code id="2"></code><img id="3" src="./image.png"/></p> <pre id="4"></pre> <p><span id="5" class="some-class"></span><span id="some-id"></span></p> <pre id="6" class='ignore'> <!-- don't ignore --> </pre> </body> </html> """ soup = self.soup(markup, 'html5lib') ids = [] for el in sv.iselect('span[id]', soup): ids.append(el.attrs['id']) self.assertEqual(sorted(['5', 'some-id']), sorted(ids))
def test_select(self): """Test select.""" markup = """ <!-- before header --> <html> <head> </head> <body> <!-- comment --> <p id="1"><code id="2"></code><img id="3" src="./image.png"/></p> <pre id="4"></pre> <p><span id="5" class="some-class"></span><span id="some-id"></span></p> <pre id="6" class='ignore'> <!-- don't ignore --> </pre> </body> </html> """ soup = bs4.BeautifulSoup(markup, 'html5lib') ids = [] for el in sv.select('span[id]', soup): ids.append(el.attrs['id']) self.assertEqual(sorted(['5', 'some-id']), sorted(ids)) ids = [] for el in sv.select('span[id]', soup, limit=1): ids.append(el.attrs['id']) self.assertEqual(sorted(['5']), sorted(ids)) self.assertEqual( sv.select('span[id]', soup, limit=1)[0].attrs['id'], sv.select_one('span[id]', soup).attrs['id']) self.assertEqual(None, sv.select_one('h1', soup)) ids = [] for el in sv.iselect('span[id]', soup): ids.append(el.attrs['id']) self.assertEqual(sorted(['5', 'some-id']), sorted(ids)) span = sv.select('span[id]', soup)[0] ids = [] for el in sv.select('span[id]:not(#some-id)', span.parent): ids.append(el.attrs['id']) self.assertEqual(sorted(['5']), sorted(ids))