如何在鼠标悬停时获得工具提示/帮助文本的文本



我正在尝试获取工具提示/帮助文本上的文本/内容。我用的是硒爪哇。

预期结果:获取文本,以便我可以通过assertEquals 进行验证

实际结果:没有得到文本。它正在获取null值。无此类错误

HTML代码:

<lightning-primitive-bubble class="slds-popover slds-popover_tooltip slds-nubbin_bottom-left slds-fall-into-ground" role="tooltip" id="salesforce-lightning-tooltip-bubble_8869b6bc-5567-f877-4ce1-037d1eaa3c37" style="position: fixed; min-width: 75px; z-index: 9104; left: 421px; right: auto; top: 114px;" data-position-id="lgcp-1000014"><div class="slds-popover__body">When did the current support plan begin?</div></lightning-primitive-bubble>

鼠标悬停成功。输出中仅检索不到帮助文本的文本这是我的代码

String help = driver.findElement(By.xpath("(//lightning-primitive-bubble/div)[1]")).getText();
System.out.printf("Help text >", help);

我尝试过很多东西,比如我尝试过完整的xpath、绝对的xpath和css选择器等。还尝试了多种方法.getText((。getAttribute("innerText"(、.getAttribute("textContent"(等。

引用以下链接无法在Selenium WebDriver中使用gettext提取文本,也无法点击

https://www.seleniumeasy.com/selenium-tutorials/how-to-verify-tooltip-text-with-selenium-webdriver-using-java

有人能帮我拿一下吗

我找到了解决方案,并开始学习

因此,web元素属性值是动态的,它在悬停前、悬停时和悬停后不断更改其值。因此,我无法追踪其中的文本。因此,我建议的第一件事是分析属性值,并选择所需的值。

悬停前的代码:

<lightning-primitive-bubble class="slds-hide slds-popover slds-popover_tooltip slds-fall-into-ground slds-nubbin_bottom-left" style="position: absolute; min-width: 75px;" role="tooltip" id="salesforce-lightning-tooltip-bubble_e3b7781a-384d-c9bf-0b46-4d6fa6b1bba4"><div class="slds-popover__body"></div></lightning-primitive-bubble>
<div class="slds-popover__body"></div>
</lightning-primitive-bubble>

悬停:类和id都发生了更改例如class="0";slds popover slds-popover_toltip slds-nubbin_bottom-left slds从地面升起";

悬停后:

<lightning-primitive-bubble class="slds-popover slds-popover_tooltip slds-nubbin_top-left slds-fall-into-ground slds-hide" style="position: fixed; min-width: 75px; z-index: 9100; left: 421px; right: auto; top: 100px;" role="tooltip" id="salesforce-lightning-tooltip-bubble_aaddfd6a-2e8f-0462-d8f5-4574cd12e11f"
data-position-id="lgcp-1000002">
<div class="slds-popover__body">When did the current support plan begin?</div>
</lightning-primitive-bubble>

所以在我的例子中,工具提示文本出现在鼠标悬停和鼠标悬停之后。所以我选择了鼠标悬停时的动态元素

//Hover on help text
we = driver.findElement(By.xpath("(//lightning-helptext)[3]"));
actions.moveToElement(we).build().perform();
//Found the dynamic web element on mouse hover     
String actualHelpText = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class="slds-popover slds-popover_tooltip slds-nubbin_bottom-left slds-rise-from-ground"]"))).getText();
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class="slds-popover slds-popover_tooltip slds-nubbin_bottom-left slds-rise-from-ground"]"))).getText());
assertEquals(actualHelpText,"When did the current support plan begin?");

如果你在这方面得到帮助,请点击投票找到答案。

最新更新