【发布时间】:2022-01-04 08:09:07
【问题描述】:
我需要验证是否将鼠标悬停在使用 selenium 的 web 元素上。我知道有很多答案要求使用动作类或 getTitle()。例如:https://www.guru99.com/verify-tooltip-selenium-webdriver.html 在这两种解决方案中,都是关于获取文本并对其进行断言。但我的问题是它如何确保悬停在工作上(我的意思是,当用户悬停在工具提示文本上时应该显示)。例如:在下面的代码中,Actions 类用于 clickAndHold 和 moveToElement。然后 getText() 完成以将鼠标悬停在文本上。不使用 Actions 类,最终结果是不是和使用 WebElement.getText() 一样?
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.*;
public class JqueryToolTip {
public static void main(String[] args) {
String baseUrl = "http://demo.guru99.com/test/tooltip.html";
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String expectedTooltip = "What's new in 3.2";
driver.get(baseUrl);
WebElement download = driver.findElement(By.xpath(".//*[@id='download_now']"));
Actions builder = new Actions (driver);
builder.clickAndHold().moveToElement(download);
builder.moveToElement(download).build().perform();
WebElement toolTipElement = driver.findElement(By.xpath(".//*[@class='box']/div/a"));
String actualTooltip = toolTipElement.getText();
System.out.println("Actual Title of Tool Tip "+actualTooltip);
if(actualTooltip.equals(expectedTooltip)) {
System.out.println("Test Case Passed");
}
driver.close();
}
}
【问题讨论】:
-
当您将鼠标悬停在某个元素上时,如果它显示工具文本,则捕获该元素并验证文本,如果它是按钮或链接,则捕获文本/标题并验证。
标签: selenium hover tooltip display mouseover