Skip to content

Commit 780f6c2

Browse files
committed
Better error messages when converting from XPath to CSS
1 parent 20455f9 commit 780f6c2

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

seleniumbase/fixtures/xpath_to_css.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _handle_brackets_in_strings(xpath):
5656
return new_xpath
5757

5858

59-
def _filter_xpath_grouping(xpath):
59+
def _filter_xpath_grouping(xpath, original):
6060
"""
6161
This method removes the outer parentheses for xpath grouping.
6262
The xpath converter will break otherwise.
@@ -71,20 +71,28 @@ def _filter_xpath_grouping(xpath):
7171
index = xpath.rfind(")")
7272
index_p1 = index + 1 # Make "flake8" and "black" agree
7373
if index == -1:
74-
raise XpathException("Invalid or unsupported Xpath: %s" % xpath)
74+
raise XpathException(
75+
"\nInvalid or unsupported XPath:\n%s\n"
76+
"(Unable to convert XPath Selector to CSS Selector)"
77+
"" % original
78+
)
7579
xpath = xpath[:index] + xpath[index_p1:]
7680
return xpath
7781

7882

79-
def _get_raw_css_from_xpath(xpath):
83+
def _get_raw_css_from_xpath(xpath, original):
8084
css = ""
8185
attr = ""
8286
position = 0
8387

8488
while position < len(xpath):
8589
node = prog.match(xpath[position:])
8690
if node is None:
87-
raise XpathException("Invalid or unsupported Xpath: %s" % xpath)
91+
raise XpathException(
92+
"\nInvalid or unsupported XPath:\n%s\n"
93+
"(Unable to convert XPath Selector to CSS Selector)"
94+
"" % original
95+
)
8896
match = node.groupdict()
8997

9098
if position != 0:
@@ -135,6 +143,7 @@ def _get_raw_css_from_xpath(xpath):
135143

136144

137145
def convert_xpath_to_css(xpath):
146+
original = xpath
138147
xpath = xpath.replace(" = '", "='")
139148

140149
# **** Start of handling special xpath edge cases instantly ****
@@ -204,7 +213,7 @@ def convert_xpath_to_css(xpath):
204213
xpath = xpath.replace(swap, "_STAR_=")
205214

206215
if xpath.startswith("("):
207-
xpath = _filter_xpath_grouping(xpath)
216+
xpath = _filter_xpath_grouping(xpath, original)
208217

209218
css = ""
210219
if "/descORself/" in xpath and ("@id" in xpath or "@class" in xpath):
@@ -213,10 +222,12 @@ def convert_xpath_to_css(xpath):
213222
for xpath_section in xpath_sections:
214223
if not xpath_section.startswith("//"):
215224
xpath_section = "//" + xpath_section
216-
css_sections.append(_get_raw_css_from_xpath(xpath_section))
225+
css_sections.append(_get_raw_css_from_xpath(
226+
xpath_section, original)
227+
)
217228
css = "/descORself/".join(css_sections)
218229
else:
219-
css = _get_raw_css_from_xpath(xpath)
230+
css = _get_raw_css_from_xpath(xpath, original)
220231

221232
attribute_defs = re.findall(r"(\[\w+\=\S+\])", css)
222233
for attr_def in attribute_defs:

0 commit comments

Comments
 (0)