Skip to content

Commit 664df49

Browse files
committed
Update xpath_to_css.py
1 parent cbdc7b3 commit 664df49

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

seleniumbase/fixtures/xpath_to_css.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def _filter_xpath_grouping(xpath):
7676

7777
def _get_raw_css_from_xpath(xpath):
7878
css = ""
79+
attr = ""
7980
position = 0
8081

8182
while position < len(xpath):
@@ -108,7 +109,9 @@ def _get_raw_css_from_xpath(xpath):
108109
attr = '[%s*="%s"]' % (match['cattr'].replace("@", ""),
109110
match['cvalue'])
110111
elif match['cattr'] == "text()":
111-
attr = ":contains(%s)" % match['cvalue']
112+
attr = ':contains("%s")' % match['cvalue']
113+
elif match['cattr'] == ".":
114+
attr = ':contains("%s")' % match['cvalue']
112115
else:
113116
attr = ""
114117

@@ -128,6 +131,20 @@ def _get_raw_css_from_xpath(xpath):
128131
def convert_xpath_to_css(xpath):
129132
if xpath[0] != '"' and xpath[-1] != '"' and xpath.count('"') % 2 == 0:
130133
xpath = _handle_brackets_in_strings(xpath)
134+
xpath = xpath.replace("descendant-or-self::*/", "descORself/")
135+
xpath = xpath.replace(" = '", "='")
136+
if " and contains(@" in xpath and xpath.count(" and contains(@") == 1:
137+
spot1 = xpath.find(" and contains(@")
138+
spot1 = spot1 + len(" and contains(@")
139+
spot2 = xpath.find(",", spot1)
140+
attr = xpath[spot1:spot2]
141+
swap = " and contains(@%s, " % attr
142+
if swap in xpath:
143+
swap_spot = xpath.find(swap)
144+
close_paren = xpath.find(']', swap_spot) - 1
145+
if close_paren > 1:
146+
xpath = xpath[:close_paren] + xpath[close_paren+1:]
147+
xpath = xpath.replace(swap, "_STAR_=")
131148

132149
if xpath.startswith('('):
133150
xpath = _filter_xpath_grouping(xpath)
@@ -149,4 +166,16 @@ def convert_xpath_to_css(xpath):
149166
css = css.replace('_STR_L_bracket_', '\\[')
150167
css = css.replace('_STR_R_bracket_', '\\]')
151168

169+
# Handle a lot of edge cases with conversion
170+
css = css.replace(" > descORself > ", ' ')
171+
css = css.replace(" descORself > ", ' ')
172+
css = css.replace("/descORself/*", ' ')
173+
css = css.replace("/descORself/", ' ')
174+
css = css.replace("descORself/", ' ')
175+
css = css.replace("_STAR_=", "*=")
176+
css = css.replace("]/", "] ")
177+
css = css.replace("] *[", "] > [")
178+
css = css.replace("\'", '"')
179+
css = css.replace("[@", '[')
180+
152181
return css

0 commit comments

Comments
 (0)