Skip to content

Commit 329d272

Browse files
committed
Handle edge case: Brackets that are part of strings
1 parent 9c39459 commit 329d272

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

integrations/selenium_ide/convert_ide.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def main():
131131
if data:
132132
whitespace = data.group(1)
133133
css_selector = '#%s' % data.group(2)
134+
css_selector = css_selector.replace('[', '\\[').replace(']', '\\]')
134135
command = '''%sself.click('%s')''' % (whitespace, css_selector)
135136
seleniumbase_lines.append(command)
136137
continue
@@ -142,6 +143,7 @@ def main():
142143
if data:
143144
whitespace = data.group(1)
144145
css_selector = '#%s' % data.group(2)
146+
css_selector = css_selector.replace('[', '\\[').replace(']', '\\]')
145147
text = data.group(3)
146148
command = '''%sself.update_text('%s', '%s')''' % (
147149
whitespace, css_selector, text)
@@ -156,6 +158,7 @@ def main():
156158
uses_keys = True
157159
whitespace = data.group(1)
158160
css_selector = '#%s' % data.group(2)
161+
css_selector = css_selector.replace('[', '\\[').replace(']', '\\]')
159162
key = 'Keys.%s' % data.group(3)
160163
command = '''%sself.send_keys('%s', %s)''' % (
161164
whitespace, css_selector, key)

seleniumbase/fixtures/xpath_to_css.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3859
def _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

110131
def 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

Comments
 (0)