Getting text between <span> Text </span> in Selenium C #

I ran into the problem of getting the subject line for a subject from unread emails using Selenium webdriver-C #.

Here is the HTML code:

<div class="ae4 UI UJ" gh="tl"> <div class="Cp"> <div> <table id=":8e" class="F cf zt" cellpadding="0"> <colgroup> <tbody> <tr id=":8d" class="zA zE"> <td class="PF xY"></td> <td id=":8c" class="oZ-x3 xY" style=""> <td class="apU xY"> <td class="WA xY"> <td class="yX xY "> <td id=":87" class="xY " role="link" tabindex="0"> <div class="xS"> <div class="xT"> <div id=":86" class="yi"> <div class="y6"> **<span id=":85"> <b>hi</b> </span>** <span class="y2"> </div> </div> </div> </td> <td class="yf xY "> </td> <td class="xW xY "> </tr> 

I can print "emailSenderName" in the console, but could not print "text" (the subject line, that is, "hello" in this case), since it is between the span tags. Here is my code.

  //Try to Retrieve mail Senders name and Subject IWebElement tbl_UM = d1.FindElement(By.ClassName("Cp")).FindElement(By.ClassName("F")); IList<IWebElement> tr_ListUM = tbl_UM.FindElements(By.ClassName("zE")); Console.WriteLine("NUMBER OF ROWS IN THIS TABLE = " + tr_ListUM.Count()); foreach (IWebElement trElement in tr_ListUM) { IList<IWebElement> td_ListUM = trElement.FindElements(By.TagName("td")); Console.WriteLine("NUMBER OF COLUMNS=" + td_ListUM.Count()); string emailSenderName = td_ListUM[4].FindElement(By.ClassName("yW")).FindElement(By.ClassName("zF")).GetAttribute("name"); Console.WriteLine(emailSenderName); string text = td_ListUM[5].FindElement(By.ClassName("y6")).FindElement(By.TagName("span")).FindElement(By.TagName("b")).Text; Console.WriteLine(text); } 

I also tried just selecting the text from the tag of the 5th column (td), which contains the text of the topic (in my case), but no results.

I could go wrong, or maybe there is another way to do it.

Please offer, in advance :)

+4
source share
4 answers

try it

 findElement(By.cssSelector("div.y6>span>b")).getText(); 
+2
source

The getText method available in the Java Selenium Web Driver implementation seems to be better than the equivalent Text property for C #.

I found a way to reach the same end, which, although somewhat confusing, works well:

 public static string GetInnerHtml(this IWebElement element) { var remoteWebDriver = (RemoteWebElement)element; var javaScriptExecutor = (IJavaScriptExecutor) remoteWebDriver.WrappedDriver; var innerHtml = javaScriptExecutor.ExecuteScript("return arguments[0].innerHTML;", element).ToString(); return innerHtml; } 

It works by passing IWebElement as a parameter to execute JavaScript in a browser that treats it like a regular DOM element. Then you can access the properties on it, for example, "innerHTML".

I tested this only in Google Chrome, but I see no reason why this should not work in other browsers.

+6
source

Using GetAttribute("textContent") instead of Text() did the trick for me.

 Driver.FindElement(By.CssSelector("ul.list span")).GetAttribute("textContent") 
+4
source

I had the same problem. Worked on PhantomJS. The solution is to get the value using GetAttribute ("textContent"):

 Driver.FindElementsByXPath("SomexPath").GetAttribute("textContent"); 
+1
source

All Articles