【问题标题】:How to locate and get text from a tooltip?如何从工具提示中定位和获取文本?
【发布时间】:2019-01-14 00:44:13
【问题描述】:

我正在尝试在 div 中定位工具提示,然后提取当我将鼠标悬停在工具提示上时可见的工具提示文本。 从 UI 复制下面的代码快照 --

<div class="w-table w-table-hover ng-star-inserted">
    <table class="w-table  w-table-hover">
        <tbody>
            <tr class="w-table-border">
                <td class="w-table-row pl-3 w-table-width-60">
                    John Spiderman (123456)
                </td>
                <td class="w-table-row pl-2 w-table-width-25">
                    Open
                </td>
                <td class="w-table-row text-right px-2 w-table-width-15">
                    <!---->
                    <div class="w-table-row-text ng-star-inserted">
                        Unavailable
                        <i class="fa fa-question-circle w-icon-question-circle" 
                           container="body"
                           placement="right"
                           id="tooltip-report-account-123456">
                        </i>
                    </div>
                    <!---->
                </td>
           </tr>
       </tbody>
   </table>

我尝试执行以下操作来定位元素,然后从中获取文本,但是它不起作用,因为文本仅在鼠标悬停时显示

var tooltipElement = driver.FindElement(By.Id("tooltip-report-account-" + accountNumber));
Assert.AreEqual(tooltipElement.Text.ToLower().Trim(), 'This account is unavailable');

找不到元素

【问题讨论】:

  • 为什么不将帐号存储在上下文中并从中检索。据我记得工具提示仅适用于鼠标悬停。你检查过这个link
  • locate a tooltip within a div 大概文本 Unavailable 似乎是工具提示,但您不一定将鼠标悬停在工具提示上,您需要更新您需要鼠标悬停的元素的问题

标签: c# selenium selenium-webdriver


【解决方案1】:

我猜This account is 字符串是 javascript 函数中的前缀,为什么不直接与来自该元素 ID 父级的文本 Unavailable 进行比较。

var tooltipElement = driver.FindElement(By.XPath("//i[@id='tooltip-report-account-" + accountNumber + "']/parent::div"));
Assert.AreEqual(tooltipElement.Text.ToLower().Trim(), 'unavailable');

【讨论】:

    【解决方案2】:

    您需要先使用动作类执行鼠标悬停,然后尝试定位显示工具提示的元素

    WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
    var element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(<element xpath>)));
    Actions action = new Actions(Driver);
    action.MoveToElement(element).Perform();   
    //Waiting for the menu to be displayed    
    System.Threading.Thread.Sleep(4000);
    

    【讨论】:

      【解决方案3】:

      如果您无法找到该元素;那么可能使用的定位器有问题。 尝试使用以下 xpath 一次:

      var tooltipElement = driver.FindElement(By.Xpath("//*[contains(@id,'tooltip-report-account')]"));
      

      【讨论】:

        猜你喜欢
        • 2010-11-22
        • 2021-07-24
        • 1970-01-01
        • 2017-02-10
        • 1970-01-01
        • 2013-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多