Handle edge case: Brackets that are part of strings
This commit is contained in:
parent
9c39459d73
commit
329d27287b
|
@ -131,6 +131,7 @@ def main():
|
|||
if data:
|
||||
whitespace = data.group(1)
|
||||
css_selector = '#%s' % data.group(2)
|
||||
css_selector = css_selector.replace('[', '\\[').replace(']', '\\]')
|
||||
command = '''%sself.click('%s')''' % (whitespace, css_selector)
|
||||
seleniumbase_lines.append(command)
|
||||
continue
|
||||
|
@ -142,6 +143,7 @@ def main():
|
|||
if data:
|
||||
whitespace = data.group(1)
|
||||
css_selector = '#%s' % data.group(2)
|
||||
css_selector = css_selector.replace('[', '\\[').replace(']', '\\]')
|
||||
text = data.group(3)
|
||||
command = '''%sself.update_text('%s', '%s')''' % (
|
||||
whitespace, css_selector, text)
|
||||
|
@ -156,6 +158,7 @@ def main():
|
|||
uses_keys = True
|
||||
whitespace = data.group(1)
|
||||
css_selector = '#%s' % data.group(2)
|
||||
css_selector = css_selector.replace('[', '\\[').replace(']', '\\]')
|
||||
key = 'Keys.%s' % data.group(3)
|
||||
command = '''%sself.send_keys('%s', %s)''' % (
|
||||
whitespace, css_selector, key)
|
||||
|
|
|
@ -35,6 +35,27 @@ class XpathException(Exception):
|
|||
pass
|
||||
|
||||
|
||||
def _handle_brackets_in_strings(xpath):
|
||||
# Edge Case: Brackets in strings.
|
||||
# Example from GitHub.com -
|
||||
# '<input type="text" id="user[login]">' => '//*[@id="user[login]"]'
|
||||
# Need to tell apart string-brackets from regular brackets
|
||||
new_xpath = ""
|
||||
chunks = xpath.split('"')
|
||||
len_chunks = len(chunks)
|
||||
for chunk_num in range(len_chunks):
|
||||
if chunk_num % 2 != 0:
|
||||
chunks[chunk_num] = chunks[chunk_num].replace(
|
||||
'[', '_STR_L_bracket_')
|
||||
chunks[chunk_num] = chunks[chunk_num].replace(
|
||||
']', '_STR_R_bracket_')
|
||||
new_xpath += chunks[chunk_num]
|
||||
if chunk_num != len_chunks - 1:
|
||||
new_xpath += '"'
|
||||
xpath = new_xpath
|
||||
return xpath
|
||||
|
||||
|
||||
def _filter_xpath_grouping(xpath):
|
||||
"""
|
||||
This method removes the outer parentheses for xpath grouping.
|
||||
|
@ -108,6 +129,9 @@ def _get_raw_css_from_xpath(xpath):
|
|||
|
||||
|
||||
def convert_xpath_to_css(xpath):
|
||||
if xpath[0] != '"' and xpath[-1] != '"' and xpath.count('"') % 2 == 0:
|
||||
xpath = _handle_brackets_in_strings(xpath)
|
||||
|
||||
if xpath.startswith('('):
|
||||
xpath = _filter_xpath_grouping(xpath)
|
||||
|
||||
|
@ -124,4 +148,8 @@ def convert_xpath_to_css(xpath):
|
|||
new_attr_def = attr_def[:q1] + "'" + attr_def[q1:q2] + "']"
|
||||
css = css.replace(attr_def, new_attr_def)
|
||||
|
||||
# Replace the string-brackets with escaped ones
|
||||
css = css.replace('_STR_L_bracket_', '\\[')
|
||||
css = css.replace('_STR_R_bracket_', '\\]')
|
||||
|
||||
return css
|
||||
|
|
Loading…
Reference in New Issue