【问题标题】:How to verify the mouse hover message in Selenium如何在 Selenium 中验证鼠标悬停消息
【发布时间】:2018-04-11 12:53:26
【问题描述】:

我尝试使用以下代码打印鼠标悬停消息:

WebElement element = driver.findElement(By.xpath("/html/body/div/form/div/div/div/div/div[1]/div/div/div"));   
Actions action = new Actions(driver);
action.moveToElement(element).build().perform();
WebElement toolTipElement = driver.findElement(By.xpath("/html/body/div/form/div/div/div/div/div[1]/div/div/div"));
String toolTipTxt = toolTipElement.getText();
System.out.println(toolTipTxt);

实际结果:

悬停在图标上,不打印悬停消息,它会跳到下一个。

【问题讨论】:

  • 请用相关的HTML更新问题。

标签: java selenium selenium-webdriver hover webdriver


【解决方案1】:

用于打印当我们悬停鼠标时弹出的消息基本上是html中的标题属性。我为您的问题创建了一个 HTML 文件,并为此编写了代码。

HTML 代码:

<html>
<head>
<title>StackOverFlow Problems </title>
</head>
<body>
<h2>Hovering get Text</h2>
<span title="hoverin' words">This will show tool-tip</span>
</body>
</html>   

获取悬停消息并打印它的 Selenium 代码。

Selenium + java 代码:

public class HoverGetText {

    static WebDriver driver;
    static WebDriverWait wait;

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        wait = new WebDriverWait(driver, 10);
        driver.get("C:\\Users\\HunteR\\Desktop\\Automation\\abc.html");
        String hoverValue = driver.findElement(By.xpath("//span[text()='This will show tool-tip']")).getAttribute("title");
        System.out.println(hoverValue);
        }
 }  

就是这样!您可以在 WebElement 上调用 getAttribute(String args0) 方法并提供所需的 HTML 属性。

如果您有任何疑问,请告诉我。

【讨论】:

  • 我已经尝试了上面的代码,但它正在重定向到文件路径(html文件路径)
  • 在系统中的任何位置创建一个文件夹并将 html 代码粘贴到一个文件中并将该文件重命名为 abc.html 和 driver.get("give the full path of abc.html ") ,希望对你有帮助!
  • 是的,即使我也尝试了您提到的相同步骤,但它正在重定向到浏览器并搜索文件路径
  • 重定向到浏览器?我没有明白这一点!你把你的html文件放在哪里了?
  • 我已放置在“d 文件夹”中,并在运行脚本时打开浏览器并在浏览器的 url 部分获取 html 文件路径
【解决方案2】:

我已经使用了下面的代码,它对我来说工作正常

WebElement element = driver.findElement(By.xpath("/html/body/div/form/div/div/div/div/div[1]/div/div/div")); 
        Actions action = new Actions(driver); action.moveToElement(element).build().perform(); 
    WebElement toolTipElement = driver.findElement(By.xpath("/html/body/div/form/div/div/div/div/div[1]/div/div/div")); if(driver.getPageSource().contains("Hover message")) { System.out.println("Available"); 
    } else
    {
    System.out.println("Not Available"); 
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多