在Selenium C中检索<span> Text </span>之间的文本#

时间:2012-12-27 16:16:30

标签: c# selenium-webdriver html

我在使用Selenium webdriver-C#从未读邮件中检索邮件的主题标题时遇到问题。

这是HTML代码:

<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>

我能够在控制台中打印'emailSenderName',但无法打印'text'(主题行,在这种情况下为“hi”),因为它位于span标签之间。这是我的代码。

 //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);
        }

我还试过直接从第5列(td)的标签中选择Text,其中包含主题文本(在我的例子中),但没有结果。

我可能在某处出错或者可能有其他方法可以做到。

请建议,提前致谢:)

6 个答案:

答案 0 :(得分:6)

Selenium Web Driver的Java实现中提供的'getText'方法似乎比C#中提供的等效'Text'属性做得更好。

我找到了一种达到同样目的的方法,虽然有点令人费解,但效果很好:

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;
}

它的工作原理是将IWebElement作为参数传递给在浏览器中执行的某些JavaScript,它将其视为普通的DOM元素。然后,您可以访问它上面的属性,例如'innerHTML'。

我只是在谷歌浏览器中对此进行了测试,但我认为没有理由认为这不适用于其他浏览器。

答案 1 :(得分:4)

使用GetAttribute("textContent")代替Text()为我做了诀窍。

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

答案 2 :(得分:1)

试试这个

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

答案 3 :(得分:1)

我遇到了同样的问题。致力于PhantomJS。解决方案是使用GetAttribute获取值(&#34; textContent&#34;):

Driver.FindElementsByXPath("SomexPath").GetAttribute("textContent");

答案 4 :(得分:0)

这在Visual Studio 2017单元测试项目中为我工作。我正在尝试从预输入控件中找到搜索结果。

using seconds = std::chrono::seconds; //std::duration<int64_t, std::ratio<1,1>>
using milliseconds = std::chrono::milliseconds; //std::duration<int64_t, std::ratio<1,1000>>
using nanoseconds = std::chrono::nanoseconds; //std::duration<int64_t, std::ratio<1,1000000000>>

seconds time1 = 5s; //underlying integer value is 5.
milliseconds time2 = 5ms; //underlying integer value is 5.
time2 = time1; //underlying integer value for time2 is 5000, for time1 is still 5.
time2 = 17ms; //underlying integer value is 17.
//time1 = time2; //forbidden; tick size of time2 is smaller than time1
time1 = std::chrono::duration_cast<seconds>(time2); 
//underlying integer value for time1 is 0, due to truncation; time2 is still 17.
nanoseconds time3 = 5ms; //underlying integer value is 5000000.
time1 = 2s; //underlying integer value is 2.
time3 = time1; //underlying integer value is 2000000000.

答案 5 :(得分:0)

可能为时已晚,但可能对某人有所帮助。

        IWebElement spanText= driver.FindElement(By.XPath("//span[contains(text(), 'TEXT TO LOOK FOR')]"));
        spanText.Click();


        IWebElement spanParent= driver.FindElement(By.XPath("//span[contains(text(), 'TEXT TO LOOK FOR')]/ancestor::li"));
        spanParent.FindElement(By.XPath(".//a[contains(text(), 'SIBLING LINK TEXT')]")).Click();

此处的额外内容可查找此文本的兄弟姐妹 找到span元素后,从父级开始查找同级。我在这里寻找锚链接。 XPath开头的点表示您从元素spanParent开始看

<li>
<span> TEXT TO LOOK FOR </span>
<a>SIBLING LINK TEXT</a>
</li>
相关问题