Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Why is sbase is complaining about this selector? #1188

Answered by mdmintz
maburrub asked this question in Q&A
Discussion options

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?

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
2 replies
Comment options

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!!

Comment options

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

Answer selected by mdmintz
Comment options

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>

You must be logged in to vote
1 reply
Comment options

div//input is an xpath selector, and you are mixing both CSS with XPath, which gave you an invalid selector.

Comment options

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

You must be logged in to vote
4 replies
Comment options

The selector for that input field is:
'input[data-testid="inputFieldTextInputBox"]'

Comment options

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 ?

Comment options

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

Comment options

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /