-
Notifications
You must be signed in to change notification settings - Fork 1.4k
-
When using a selector like:
"//div[./span[text()='Number32866224520']]/following-sibling::div//input"
SeleniumBase generates the following error:
[undefined]seleniumbase.fixtures.xpath_to_css.XpathException: Invalid or unsupported Xpath:
//div[./span[text()='Number32866224520']]/following-sibling::div/descORself/input
Why is this selector is invalid?
Beta Was this translation helpful? Give feedback.
All reactions
When you call a SeleniumBase method that requires a Javascript call, SeleniumBase needs to first convert any XPath selector to a CSS Selector in order for it to work, but for a complex XPath selector like the one you have above, there might not be a simple conversion that's possible. For that situation, you should try using a CSS Selector instead, or use an XPath selector that can be easily converted to a CSS Selector.
Replies: 3 comments 7 replies
-
When you call a SeleniumBase method that requires a Javascript call, SeleniumBase needs to first convert any XPath selector to a CSS Selector in order for it to work, but for a complex XPath selector like the one you have above, there might not be a simple conversion that's possible. For that situation, you should try using a CSS Selector instead, or use an XPath selector that can be easily converted to a CSS Selector.
Beta Was this translation helpful? Give feedback.
All reactions
-
Is this a limitation? Locating elements by searching for text does not work with css and that is the only way to locate that element in xpath!!
Beta Was this translation helpful? Give feedback.
All reactions
-
SeleniumBase has added the :contains()
selector for this very reason. See examples:
https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_contains_selector.py
As for the error message, I will improve on it so that in your case it would become:
seleniumbase.fixtures.xpath_to_css.XpathException:
Invalid or unsupported XPath:
//div[./span[text()='Number32866224520']]/following-sibling::div//input
(Unable to convert XPath Selector to CSS Selector)
Here's another example that uses the :contains()
selector in CSS Selectors:
https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_tinymce.py
Beta Was this translation helpful? Give feedback.
All reactions
-
What I am trying to do is to use the contains and also next sibling so if I need to resort to css then I was thinking about:
div > span:contains('some_text') + div//input
meaning find a divider that contains a span with text and get the next sibling (div//input)
but when I do that I get:
Failed: [undefined]cssselect.parser.SelectorSyntaxError: Expected selector, got <DELIM '/' at 46>
Beta Was this translation helpful? Give feedback.
All reactions
-
div//input
is an xpath selector, and you are mixing both CSS with XPath, which gave you an invalid selector.
Beta Was this translation helpful? Give feedback.
All reactions
-
Here is a screenshot that shows the html structure of the example I asked about above
Screen Shot 2022年01月28日 at 10 11 41 AM
Beta Was this translation helpful? Give feedback.
All reactions
-
The selector for that input field is:
'input[data-testid="inputFieldTextInputBox"]'
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks for pointing out the mixing issue, it was my bad (I should have used div input without //). As for 'input[data-testid="inputFieldTextInputBox"]' ? yes that is the easiest but this is a dynamically generated id so I cannot use it. With CSS I can locate the span containing the text but I can not reference its parent so that I can use + div input which is the next sibling of the parent as shown in the screenshot. In other words. Is there any way in sbase I can have an equivalent to the following xpath: "//div[./span[text()='some_text']]/following-sibling::div//input"
or how can I locate the divider containing a span with text ?
Beta Was this translation helpful? Give feedback.
All reactions
-
Are you saying "inputFieldTextInputBox"
is dynamically generated?
There's probably a better XPath that you can use. See if the SeleniumBase Recorder can generate the correct selector for you.
https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/recorder_mode.md
Beta Was this translation helpful? Give feedback.
All reactions
-
I understand it sounds weird to believe inputFieldTextInputBox is not unique but this is the case. the application creates these inputs in the fly and give the same id for all of them so I can not rely on that. I know this is not good but I have to deal with it. That is why I kept insisting on locating the span with text. That is the only anchor point I can rely on. I will give the recorder a try. Thank you.
Beta Was this translation helpful? Give feedback.