【问题标题】:No text returned for IWebElement.Text没有为 IWebElement.Text 返回文本
【发布时间】:2020-07-27 00:27:12
【问题描述】:

我有一些 xpath 可以正确返回我之后的节点,但我无法检索 Selenium 中标签之间的文本。

以下正确返回我所追求的 4 个节点:

var subMenuItems = driver.FindElements(By.XPath("//div[@id='OpenandApply123']//a"));

但是,当我执行以下操作时,我期望“abc123”时返回的文本一无所获:

string item1 = subMenuItems[0].Text;

我如何获取返回的 abc123、def123 等文本?

下面是完整的html,格式很抱歉:

<div class="subMenu" id="OpenandApply123" role="menu" aria-hidden="false" style="" xpath="1">
                            <table cellpadding="0" cellspacing="0" width="150" role="presentation">

                                    <tbody><tr role="presentation">
                                        <td role="presentation" class="">
                                            <a id="abc.feature_link" href="abc/abc" class="menuItem" target="_top" style="background-position: 4px 2px;">
                                                abc123
                                            </a>


                                        </td>
                                    </tr>

                                    <tr role="presentation">
                                        <td role="presentation" class="">
                                            <a id="def.feature_link" href="abc/Feature/def" class="menuItem" target="_top" style="background-position: 4px 2px;">
                                                def123
                                            </a>


                                        </td>
                                    </tr>

                                    <tr role="presentation">
                                        <td role="presentation">
                                            <a id="ghi.feature_link" href="abc/Feature/ghi" class="menuItem" target="_top" style="">
                                                ghi123
                                            </a>


                                        </td>
                                    </tr>

                                    <tr role="presentation">
                                        <td role="presentation">
                                            <a id="klm.feature_link" href="abc/klm" class="menuItem" target="_top" style="">
                                                klm123
                                            </a>


                                        </td>
                                    </tr>

                            </tbody></table>
                        </div>

【问题讨论】:

    标签: c# html selenium xpath


    【解决方案1】:

    我建议诱导WebDriverWait 并等待VisibilityOfAllElementsLocatedBy() 并遵循xpath。

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//td[@role='presentation']//a")));
    var subMenuItems = driver.FindElements(By.XPath("//td[@role='presentation']//a"));
    string item1 = subMenuItems[0].Text;
    

    或者

    string item1 = subMenuItems[0].GetAttribute("textContent");
    

    【讨论】:

    • 感谢您的帖子。您能否解释一下为什么在这种情况下 .Text 不起作用但 .GetAttribute 起作用?当没有显式属性时,您是如何知道使用 GetAttribute 的?
    • 当文本不可见时.Text 在这种情况下将不起作用。但是GetAttribute("textContent") 将起作用。
    • @ratsstack : 你可以查看这个链接w3schools.com/jsref/prop_node_textcontent.asp
    • @ratsstack :如果这解决了您的问题,请接受并投票支持 clouser。谢谢。
    • 感谢您的解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多