@@ -35,6 +35,27 @@ class XpathException(Exception):
3535 pass
3636
3737
38+ def _handle_brackets_in_strings (xpath ):
39+ # Edge Case: Brackets in strings.
40+ # Example from GitHub.com -
41+ # '<input type="text" id="user[login]">' => '//*[@id="user[login]"]'
42+ # Need to tell apart string-brackets from regular brackets
43+ new_xpath = ""
44+ chunks = xpath .split ('"' )
45+ len_chunks = len (chunks )
46+ for chunk_num in range (len_chunks ):
47+ if chunk_num % 2 != 0 :
48+ chunks [chunk_num ] = chunks [chunk_num ].replace (
49+ '[' , '_STR_L_bracket_' )
50+ chunks [chunk_num ] = chunks [chunk_num ].replace (
51+ ']' , '_STR_R_bracket_' )
52+ new_xpath += chunks [chunk_num ]
53+ if chunk_num != len_chunks - 1 :
54+ new_xpath += '"'
55+ xpath = new_xpath
56+ return xpath
57+
58+
3859def _filter_xpath_grouping (xpath ):
3960 """
4061 This method removes the outer parentheses for xpath grouping.
@@ -108,6 +129,9 @@ def _get_raw_css_from_xpath(xpath):
108129
109130
110131def convert_xpath_to_css (xpath ):
132+ if xpath [0 ] != '"' and xpath [- 1 ] != '"' and xpath .count ('"' ) % 2 == 0 :
133+ xpath = _handle_brackets_in_strings (xpath )
134+
111135 if xpath .startswith ('(' ):
112136 xpath = _filter_xpath_grouping (xpath )
113137
@@ -124,4 +148,8 @@ def convert_xpath_to_css(xpath):
124148 new_attr_def = attr_def [:q1 ] + "'" + attr_def [q1 :q2 ] + "']"
125149 css = css .replace (attr_def , new_attr_def )
126150
151+ # Replace the string-brackets with escaped ones
152+ css = css .replace ('_STR_L_bracket_' , '\\ [' )
153+ css = css .replace ('_STR_R_bracket_' , '\\ ]' )
154+
127155 return css
0 commit comments