The text property on the selenium web element is empty, although I can check it with Visual Studio

Without posting code pages and c # markup, does anyone have a reason why this code

var link = _driver.FindElement(By.Id(field + "Field")); var id = link.GetAttribute("id"); var text = link.Text; 

given this markup

 <a id="ForenameField" href="/MyUrl/MyFolder/MyId">3 errors</a> 

Assigns an empty line to a text variable, but if I put a breakpoint on the second or third line and check the reference variable, I can see the inner text of the element with respect to the Text property on the inspector, it reads "3 errors", but the text value is an empty line. It is not hidden, can I see the text if I add a clock or use quickview, any ideas?

+1
source share
2 answers

Good, this is bad. Using jquery to switch the class in the div containing the html in the question meant that although users see the div appear, the class that hides the div is still in the tag. A bit like this

 <div class="hideThis showThis"><!-- my elements /--></div> 

This makes Selenium right does not give me text value. It is strange, however, that the Visual Studio debugger believes that there should be a value. Visual Studio seems to come with what I see, but Selenium is more pedantic in that this hideThis class exists.

I agree that if you can’t see it, you can’t interact with it, so it’s worth looking at the html graph from the element, which, as you expect, will matter to see if any class is present, will hide your item.

Feel free to recommend me to remove this pretty obvious wisdom.

+2
source

I know that this was published over a year ago, but I also had this exact problem and came across this topic. I was able to solve this by simply waiting for the DOM to load - some elements do not appear until the DOM is updated. So just put Thread.Sleep (6000) or something else after going to the page so that it works for me.

+1
source

All Articles