Skip to content

Commit 56cf4aa

Browse files
authored
Merge pull request #687 from seleniumbase/update-xpath-to-css-selector-converter
Update the xpath-to-css selector converter
2 parents edb593d + 82ecfa1 commit 56cf4aa

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

seleniumbase/fixtures/xpath_to_css.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,50 @@ def _get_raw_css_from_xpath(xpath):
129129

130130

131131
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+
132172
if xpath[0] != '"' and xpath[-1] != '"' and xpath.count('"') % 2 == 0:
133173
xpath = _handle_brackets_in_strings(xpath)
134174
xpath = xpath.replace("descendant-or-self::*/", "descORself/")
135-
xpath = xpath.replace(" = '", "='")
175+
136176
if " and contains(@" in xpath and xpath.count(" and contains(@") == 1:
137177
spot1 = xpath.find(" and contains(@")
138178
spot1 = spot1 + len(" and contains(@")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
setup(
5656
name='seleniumbase',
57-
version='1.49.5',
57+
version='1.49.6',
5858
description='Web Automation and Test Framework - https://seleniumbase.io',
5959
long_description=long_description,
6060
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)