我试图在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');
找不到元素
我猜字符串<code>Thisaccountis
var tooltipElement = driver.FindElement(By.XPath("//i[@id='tooltip-report-account-" + accountNumber + "']/parent::div"));
Assert.AreEqual(tooltipElement.Text.ToLower().Trim(), 'unavailable');
您需要首先使用操作类执行鼠标悬停,然后尝试查找显示工具提示的元素
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);
如果找不到元素;那么也许使用的定位器有一些问题。尝试使用以下 xpath 一次:
var tooltipElement = driver.FindElement(By.Xpath("//*[contains(@id,'tooltip-report-account')]"));