_OPTION_1 = "Average"
_OPTION_2 = "M"
_OPTION_3 = "Casual"
_OPTION_4 = "D"
_WIDTH_VALUE_XPATH = f"//mat-option/span[contains(text(),'{_OPTION_1}')] | //mat-option/span[contains(text(),'{_OPTION_2}')] | //mat-option/span[contains(text(),'{_OPTION_3}')] | //mat-option/span[contains(text(),'{_OPTION_4}')]"
2 Answers
You can use //mat-option/span[condition1 or condition2 or condition3]
With XPath 2.0 or higher you could also use
//mat-option/span[matches(., 'aaa|bbb|ccc')]
But sadly the developers of Selenium are still stuck on XPath 1.0.
Are you sure you want "text()" here rather than "."? With text()
, the text must all be in one text node: no intervening comments or element tags allowed.
-
Can we try something with regular expression here? And yes I am sure with text() but I will do consider using. as it will make the XPath Robust. Apr 21, 2021 at 7:23
-
-
@PDHide what you mean is, regex is not supported in XPath 1.0. Regular expressions have been supported in XPath since 2007, but a lot of people (including Selenium users) are still using the older version. Apr 22, 2021 at 8:34
-
@MichaelKay xpath 2 is not supported in browser , browser still supports only xpath 1.0 so is selenium . So its not what selenium users thinks but thats the only option we have unfortunately– PDHide ♦Apr 22, 2021 at 8:41
-
Selenium could support XPath 2.0 or 3.1 if it chose to, even though there's no native support in the browser. Apr 24, 2021 at 8:38
https://developer.mozilla.org/en-US/docs/Web/XPath/Functions
Browser supports only XPATH version 1.0 and matches is an XPATH 2.0 method, and so you can use only xpath 1.0 in selenium. The above linked document shows the supported XPATH methods.
so in your case
//mat-option/span[text()="someting" or text()="something" and so on ]
//mat-option/span[contains(text(),"someting") or contains(text(),"someting") and so on]
//mat-option/span[contains(string(),"someting") or contains(string(),"someting") and so on]