Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Text between <span>Text</span> in Selenium C#

I am facing problem in retrieving Subject title of a mail from Unread mails using Selenium webdriver-C#.

Here's 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 am able to print 'emailSenderName' in console but unable to print 'text' (subject line i.e. "hi" in this case) as it is between span tags. Here's 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 had also tried by directly selecting the Text from tag of 5th Column (td), which contains the subject text (in my case), but no results.

I might went wrong somewhere or may be there is some other way of doing it.

Please suggest, Thanks in advance :)

like image 283
Adi Avatar asked Dec 27 '12 16:12

Adi


People also ask

How do you get SPAN text in Selenium?

To identify the element with span tag, we have to first identify it with any of the locators like xpath, css, class name or tagname. After identification of the element, we can perform the click operation on it with the help of the click method. Then obtain its text with the text method.

How do I getText inside a span XPath?

You can use By. XPath with the code you have​ Inner text is text between the opening tags and closing tags. For example: <a>I Am Inner Text</a> In above example, texts “I Am Inner Text” between opening and closing tags are called inner text of web element “a”.

How do you getText from a tag in Selenium?

The Selenium WebDriver interface has predefined the getText() method, which helps retrieve the text for a specific web element. This method gets the visible, inner text (which is not hidden by CSS) of the web-element.

How do I getText from Webelements?

We can get text from a webelement with Selenium webdriver. The getText() methods obtains the innerText of an element. It fetches the text of an element which is visible along with its sub elements. It ignores the trailing and leading spaces.


1 Answers

The 'getText' method available in the Java implementation of Selenium Web Driver seems to do a better job than the equivalent 'Text' property available in C#.

I found a way of achieving the same end which, although somewhat convoluted, 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 an IWebElement as a parameter to some JavaScript executing in the Browser, which treats it just like a normal DOM element. You can then access properties on it such as 'innerHTML'.

I've only tested this in Google Chrome but I see no reason why this shouldn't work in other browsers.

like image 107
Holf Avatar answered Sep 25 '22 11:09

Holf