Skip to content

Commit 108a76d

Browse files
author
ravishankar
committed
table parser added
1 parent ec68f24 commit 108a76d

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

s_tool/parser.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from s_tool.exceptions import SToolException
12
from s_tool.utils import get_element
23

34

@@ -14,7 +15,7 @@ def select_options(element, swap=None, text_exclude=[]):
1415
"""
1516
option_dict = dict()
1617
if element and hasattr(element, "tag_name") and element.tag_name == "select":
17-
options = get_element(element, "tag_name", "option", many=True)
18+
options = get_element(element, "option", "tag_name", many=True)
1819
for option in options:
1920
func = option.get_attribute
2021
text, value = func("text"), func("value")
@@ -24,3 +25,31 @@ def select_options(element, swap=None, text_exclude=[]):
2425
option_dict[value] = text
2526

2627
return option_dict
28+
29+
30+
def get_table(table):
31+
"""Return list of rows including header and footer of given table
32+
33+
Args:
34+
A selenium table element
35+
36+
Returns:
37+
return list of row list,
38+
return [] if table not valid
39+
40+
"""
41+
results = []
42+
if table and hasattr(table, "tag_name") and table.tag_name == "table":
43+
for rows in table.find_elements_by_tag_name("tr"):
44+
cell_data = []
45+
for cell in rows.find_elements_by_xpath("td | th"):
46+
colspan = cell.get_attribute("colspan")
47+
cell_text = cell.text
48+
if colspan:
49+
cell_data.extend([cell_text] + [""] * (int(colspan) - 1))
50+
else:
51+
cell_data.append(cell_text)
52+
results.append(cell_data)
53+
else:
54+
raise SToolException("INVALIDTABLE")
55+
return results

0 commit comments

Comments
 (0)