@@ -56,7 +56,7 @@ def _handle_brackets_in_strings(xpath):
56
56
return new_xpath
57
57
58
58
59
- def _filter_xpath_grouping (xpath ):
59
+ def _filter_xpath_grouping (xpath , original ):
60
60
"""
61
61
This method removes the outer parentheses for xpath grouping.
62
62
The xpath converter will break otherwise.
@@ -71,20 +71,28 @@ def _filter_xpath_grouping(xpath):
71
71
index = xpath .rfind (")" )
72
72
index_p1 = index + 1 # Make "flake8" and "black" agree
73
73
if index == - 1 :
74
- raise XpathException ("Invalid or unsupported Xpath: %s" % xpath )
74
+ raise XpathException (
75
+ "\n Invalid or unsupported XPath:\n %s\n "
76
+ "(Unable to convert XPath Selector to CSS Selector)"
77
+ "" % original
78
+ )
75
79
xpath = xpath [:index ] + xpath [index_p1 :]
76
80
return xpath
77
81
78
82
79
- def _get_raw_css_from_xpath (xpath ):
83
+ def _get_raw_css_from_xpath (xpath , original ):
80
84
css = ""
81
85
attr = ""
82
86
position = 0
83
87
84
88
while position < len (xpath ):
85
89
node = prog .match (xpath [position :])
86
90
if node is None :
87
- raise XpathException ("Invalid or unsupported Xpath: %s" % xpath )
91
+ raise XpathException (
92
+ "\n Invalid or unsupported XPath:\n %s\n "
93
+ "(Unable to convert XPath Selector to CSS Selector)"
94
+ "" % original
95
+ )
88
96
match = node .groupdict ()
89
97
90
98
if position != 0 :
@@ -135,6 +143,7 @@ def _get_raw_css_from_xpath(xpath):
135
143
136
144
137
145
def convert_xpath_to_css (xpath ):
146
+ original = xpath
138
147
xpath = xpath .replace (" = '" , "='" )
139
148
140
149
# **** Start of handling special xpath edge cases instantly ****
@@ -204,7 +213,7 @@ def convert_xpath_to_css(xpath):
204
213
xpath = xpath .replace (swap , "_STAR_=" )
205
214
206
215
if xpath .startswith ("(" ):
207
- xpath = _filter_xpath_grouping (xpath )
216
+ xpath = _filter_xpath_grouping (xpath , original )
208
217
209
218
css = ""
210
219
if "/descORself/" in xpath and ("@id" in xpath or "@class" in xpath ):
@@ -213,10 +222,12 @@ def convert_xpath_to_css(xpath):
213
222
for xpath_section in xpath_sections :
214
223
if not xpath_section .startswith ("//" ):
215
224
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
+ )
217
228
css = "/descORself/" .join (css_sections )
218
229
else :
219
- css = _get_raw_css_from_xpath (xpath )
230
+ css = _get_raw_css_from_xpath (xpath , original )
220
231
221
232
attribute_defs = re .findall (r"(\[\w+\=\S+\])" , css )
222
233
for attr_def in attribute_defs :
0 commit comments