@@ -129,10 +129,50 @@ def _get_raw_css_from_xpath(xpath):
129
129
130
130
131
131
def convert_xpath_to_css (xpath ):
132
+ xpath = xpath .replace (" = '" , "='" )
133
+
134
+ # **** Start of handling special xpath edge cases instantly ****
135
+
136
+ # Handle a special edge case that converts to: 'tag.class:contains("TEXT")'
137
+ c3 = "@class and contains(concat(' ', normalize-space(@class), ' '), ' "
138
+ if c3 in xpath and xpath .count (c3 ) == 1 and xpath .count ("[@" ) == 1 :
139
+ p2 = " ') and (contains(., '"
140
+ if xpath .count (p2 ) == 1 and xpath .endswith ("'))]" ) and (
141
+ xpath .count ("//" ) == 1 and (xpath .count (" ') and (" ) == 1 )):
142
+ s_contains = xpath .split (p2 )[1 ].split ("'))]" )[0 ]
143
+ s_tag = xpath .split ("//" )[1 ].split ("[@class" )[0 ]
144
+ s_class = xpath .split (c3 )[1 ].split (" ') and (" )[0 ]
145
+ return '%s.%s:contains("%s")' % (s_tag , s_class , s_contains )
146
+
147
+ # Find instance of: //tag[@attribute='value' and (contains(., 'TEXT'))]
148
+ data = re .match (
149
+ r'''^\s*//(\S+)\[@(\S+)='(\S+)'\s+and\s+'''
150
+ r'''\(contains\(\.,\s'(\S+)'\)\)\]''' , xpath )
151
+ if data :
152
+ s_tag = data .group (1 )
153
+ s_atr = data .group (2 )
154
+ s_val = data .group (3 )
155
+ s_contains = data .group (4 )
156
+ return '%s[%s="%s"]:contains("%s")' % (s_tag , s_atr , s_val , s_contains )
157
+
158
+ # Find instance of: //tag[@attribute1='value1' and (@attribute2='value2')]
159
+ data = re .match (
160
+ r'''^\s*//(\S+)\[@(\S+)='(\S+)'\s+and\s+'''
161
+ r'''\(@(\S+)='(\S+)'\)\]''' , xpath )
162
+ if data :
163
+ s_tag = data .group (1 )
164
+ s_atr1 = data .group (2 )
165
+ s_val1 = data .group (3 )
166
+ s_atr2 = data .group (4 )
167
+ s_val2 = data .group (5 )
168
+ return '%s[%s="%s"][%s="%s"]' % (s_tag , s_atr1 , s_val1 , s_atr2 , s_val2 )
169
+
170
+ # **** End of handling special xpath edge cases instantly ****
171
+
132
172
if xpath [0 ] != '"' and xpath [- 1 ] != '"' and xpath .count ('"' ) % 2 == 0 :
133
173
xpath = _handle_brackets_in_strings (xpath )
134
174
xpath = xpath .replace ("descendant-or-self::*/" , "descORself/" )
135
- xpath = xpath . replace ( " = '" , "='" )
175
+
136
176
if " and contains(@" in xpath and xpath .count (" and contains(@" ) == 1 :
137
177
spot1 = xpath .find (" and contains(@" )
138
178
spot1 = spot1 + len (" and contains(@" )
0 commit comments