Reputation: 3473
I'm trying to find a Text via Selenium, which is directly in the HTML. This can look something like this:
<br>
Uploaded.net
<img class="bbCodeImage LbImage" />
<br>
I found the Image after the Text, but even now, I can't navigate to the text: I I went to the img-Element, then tried:
var des2 = ele.FindElement(ByProxy.XPath("preceding-sibling::*"));
Interesting enough, this already returns the br-element and not the text, which is right above it. I also tried to brute force it and get all Elements, with this text:
var des2 = thread.FindElements(ByProxy.XPath("descendant::*[contains(text(), \"Uploaded.net\")]")).SelectMany(f => f.FindElements(ByProxy.XPath("descendant::*")));
foreach(var ele in des2)
{
Debug.WriteLine(ele.Text);
}
So I read all Descendants with the mentioned Text and iterate over all of them, but none of them has a Text set.
Am I missing something crucial here?
Upvotes: 0
Views: 539
Reputation: 42518
Selenium doesn't support a text node. You could however get the text with a piece of JavaScript:
string text = (string)((IJavaScriptExecutor)driver).ExecuteScript(
"return arguments[0].previousSibling.textContent.trim();", ele);
Upvotes: 1
Reputation: 9058
I dont think there is any obvious solution to this. Can offer a very very round about solution.
Get the pagesource of the page -- driver.getPageSource();
Split the pagesource by the img tag. Then split the first element of previous split by br tag. The last element of the array should now be the text.
If you have control over development of this, someone should fix the page.
Upvotes: 1