OS: Windows 7
Selenium Version: 3.0.0
Browser: Chrome
Browser Version: 57.0.2987.133
Underlying text value is returned from IWebElement Text property, regardless of transforms applied to text through CSS.
Uppercase text-transform causes IWebElement Text property to return text in all caps. The underlying HTML text may or may not be upper case.
Create a span with text-transform: uppercase
Place lower-case text in span
Open page with Selenium WebDriver
FindElement using Driver and By,TagName
Access IWebElement text property
Text is all uppercase
This is the intended expectation. Please see the w3c spec https://w3c.github.io/webdriver/webdriver-spec.html#get-element-text
If you want the non-rendered text, get the textContent property
@lmtierney Is this property available in the C# version for IWebElement? It doesn't appear to be.
@KthProg, the textContent property he's referring to is the JavaScript textContent property, which can be retrieved via IJavaScriptExecutor.ExecuteScript().
@jimevans This property isn't exposed in any standard way through Selenium WebDriver? Perhaps I can just rename this "element property textContent not exposed through Selenium IWebElement interface".
@KthProg Yes, it's exposed via ExecuteScript(). :) The contract of the getText() method (.Text property in the .NET bindings) is to return rendered visible text. If you want something more detailed than that, you're going to need to use something that allows more flexibility, like JavaScript.
@jimevans Hmm... I'm not satisfied with that lol. The actual value of the DOM should be exposed to the developer for testing IMO.
I'm really, genuinely not getting how writing one relatively simple support method is a huge burden. Can you help me understand that? The code would be something like the following:
// WARNING! Code written from memory, without the benefit of an IDE.
// May not run correctly, or even compile without modification.
// No error handling is supplied in this sample.
public static string GetTextContent(IWebElement element, IWebDriver driver)
{
IJavaScriptExecutor executor = driver as IJavaScriptExecutor;
if (executor == null)
{
return string.Empty;
}
return executor.ExecuteScript("return arguments[0].textContent;", element).ToString();
}
You could even go so far as to make it an extension method, so that it looks like a method right off of an IWebElement in C#.
You actually have a good point with the extension method. I hadn't thought of that.
My concern is more about leaving out functionality that makes perfect sense to include. What if I told you you can have a car, but first here's the steering wheel and go attach it? It might not be a burden, but you'd definitely be wondering why I didn't put it there in the first place!
Anyways, if it's a huge burden for Selenium developers to add it, then I have no problem doing it myself, just voicing my opinion.
Thanks for clarifying @jimevans
@KthProg I understand your opinion, even as I don't agree with it. The end-user-focused nature of the WebDriver API doesn't always align with how those who primarily look through the lens of "web developer" see things. This is especially so since there is a W3C specification for WebDriver now, which is nearly to the point that it's not going to be modified much further. At any rate, glad that we have a solution you can use.
@KthProg for w3c compliant webdrivers there will be a method to get a property of the element https://w3c.github.io/webdriver/webdriver-spec.html#get-element-property
Most helpful comment
This is the intended expectation. Please see the w3c spec https://w3c.github.io/webdriver/webdriver-spec.html#get-element-text
If you want the non-rendered text, get the
textContentproperty