SeleniumBase/examples/nth_child_test.py

38 lines
1.6 KiB
Python
Raw Normal View History

2020-08-13 15:41:09 +08:00
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)
2020-08-13 15:41:09 +08:00
class NthChildSelectorTests(BaseCase):
def test_locate_rows_with_colors(self):
self.open("https://xkcd.com/color/rgb/")
tbody = "center > table tbody"
2023-08-06 03:38:18 +08:00
if self.headed:
2022-06-08 11:36:34 +08:00
self.demo_mode = True
self.demo_sleep = 0.5
self.message_duration = 2.0
2022-08-27 10:01:13 +08:00
else:
2022-11-26 02:12:58 +08:00
self.demo_mode = False
self.message_duration = 0.1
2020-08-13 15:41:09 +08:00
self.highlight(tbody)
self.post_message("Part 1: Assert text in given row.")
2020-08-14 14:07:57 +08:00
self.assert_text("teal", tbody + " tr:nth-child(2)")
self.assert_text("aqua", tbody + " tr:nth-child(4)")
self.assert_text("mint", tbody + " tr:nth-child(14)")
self.assert_text("jade", tbody + " tr:nth-child(36)")
2020-08-13 15:41:09 +08:00
soup = self.get_beautiful_soup(self.get_page_source())
self.post_message("Part 2: Find row with given text.")
self.locate_first_row_with_color("rust", tbody, soup)
self.locate_first_row_with_color("azure", tbody, soup)
self.locate_first_row_with_color("topaz", tbody, soup)
def locate_first_row_with_color(self, color, tbody, soup):
rows = soup.body.table.find_all("tr")
num_rows = len(rows)
for row in range(num_rows):
row_selector = tbody + " tr:nth-child(%s)" % (row + 1)
if color in rows[row].text:
message = '"%s" found on row %s' % (color, row + 1)
self.post_message_and_highlight(message, row_selector)
return # Found row and done
2021-05-06 09:06:24 +08:00
self.post_error_message('"%s" could not be found on any row!' % color)