splinter
splinter copied to clipboard
add an alternative to find_by_text that looks at all inner text
The current implementation of find_by_text uses the following xpath: //*[text()="some text"], which only looks at the first text node within an element. This makes it difficult to query for elements with text split across multiple text nodes.
I think it would be useful to add an alternative that acts more like element.textContent(), querying against all inner text of an element rather than its first text node. This can be done with the following xpath: //*[.="some text"]
It could be called find_by_text_content or find_by_inner_text or something along those lines.
I'm happy to do the implementation and make a pull request if people agree that there's a need / desire for this
This seems like something better served by an example in the documentation for now. Since it's essentially:
some_text = "some text spread across multiple elements"
find_by_xpath(f'//*[.="{some_text}"]')
A well documented example scenario showing the limitations of find_by_text with a solution would be helpful to anyone facing the same situation.
I seem to have run into this also, where an HTML document had been built up like:
someElem.appendChild(document.createTextNode('some');
someElem.appendChild(document.createTextNode('text');
someElem.appendChild(document.createTextNode('spread');
...
In order to search for a single term in the middle of that content, this is the only XPath selector I've been able to get working in Splinter: //descendant::*/text()[contains(., "spread")].
I'm not an expert in XPath by any means, so it's possible there's a better selector; there were others that worked in the browser dev tools console, but not in Splinter. I tried the selector mentioned in the previous response and it didn't work for my scenario, so I wanted to add my solution to the mix.
Hi, is this issue up for grabs?
Hi, is this issue still open?