如何使用selenium web驱动程序获取工具提示文本

时间:2016-06-22 13:59:25

标签: webdriver

我正在尝试获取图像的工具提示文本,并希望存储在变量中。下面是HTML Code.script能够执行鼠标悬停事件并显示工具提示文本值。但无法获取工具提示文本值。请帮助我。

<img onmouseover="var relNo=getReleaseNo(); this.T_ABOVE = false; this.T_OFFSETY=-10; this.T_OFFSETX=10; this.T_WIDTH=200; this.T_DELAY=1000; return escape('&lt;table&gt;&lt;tr&gt;&lt;td align=center&gt;Welcome to the Daimler EngineeringPortal&lt;br&gt;&lt;b&gt;' + relNo + '&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;');" src="./pics/logo/logo_mini.jpg">

PFA下面提到的是selenium脚本。在脚本中,所有事情都在起作用,无法得到文本 Selenium脚本

package All_Module_Engp;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class Engp_Module {
    public static void main(String args[])
    {
        File pathToBinary = new File("C:/Users/rrimjhi/AppData/Local/Mozilla Firefox/firefox.exe");
        FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
        FirefoxProfile firefoxProfile = new FirefoxProfile();       
        WebDriver driver = new FirefoxDriver(ffBinary,firefoxProfile);
        driver.get("https://tindito.COM");
            driver.findElement(By.id("usr")).sendKeys("xyz123");
            driver.findElement(By.id("password")).sendKeys("xyz123");
            driver.findElement(By.name("login")).click();
            driver.manage().window().maximize();
//          String s = driver.getTitle();

//          System.out.println(s);
            Actions tooltip = new Actions(driver);
            driver.switchTo().frame("header");
            WebElement img1=driver.findElement(By.xpath("//img[@src='./pics/logo/logo_mini.jpg']"));
            tooltip.clickAndHold(img1).perform();

             String ToolTipText = img1.getText();

            System.out.println(ToolTipText);
    }


}

1 个答案:

答案 0 :(得分:0)

试试这个:

WebElement elementWithTooltip = driver.findElement(By.xpath("//img[@src='./pics/logo/logo_mini.jpg']"));
Actions builder = new Actions(driver);
builder.moveToElement(elementWithTooltip).build().perform();
//try to find this tool tip element and verify the text
WebElement tooltip = driver.findElement(By.id("tooltip"));
assertEquals("tooltip content", tooltip.getText());