如何在Selenium Java中处理工具提示

时间:2019-04-30 05:02:52

标签: java selenium

Tooltip image无法使用Selenium Java中的getAttribute和getText处理工具提示。 下面是代码

HTML:

<span class="exclude-syndicated-hint hint--top" data-hint="Programs like 
&quot;The Today Show&quot; are often syndicated across major networks and 
play on a variety of channels. Checking this box will only give you a 
single copy of the program.">
<i class="syndicated-hint-icon fa fa-info-circle"></i>                                   
</span>

JAVA:

@FindBy(xpath = "//i[@class='syndicated-hint-icon fa fa-info-circle']") 
public WebElement tooltip; 
public String settooltip() 
{ 
    Actions a = new Actions(driver); 
    a.moveToElement(tooltip).perform(); 
    String actualTooltip = tooltip.getAttribute("data-hint"); 
}

4 个答案:

答案 0 :(得分:1)

 // Create action class object
Actions builder=new Actions(driver);

// find the tooltip xpath
WebElement _tooltip=driver.findElement(By.xpath("//span[@class='exclude-syndicated-hint hint--top']));


// Mouse hover to that text message
builder.moveToElement(_tooltip).perform();


// Extract text from tooltip
 String tooltip_msg=_tooltip.getAttribute("data-hint");


 // Print the tooltip message just for our refrences
 System.out.println("Tooltip message is "+tooltip_msg);

答案 1 :(得分:0)

尝试一下:-

您需要在perform()类方法上调用Actions

首先,我们需要通过传递 webdriver 实例来创建新的动作构建器实例

详细了解moveToElement

有关鼠标悬停操作的更多信息,请阅读this

Actions toolAct = new Actions(driver);
toolAct.moveToElement(element).build().perform();
WebElement toolTipElement = driver.findElement(By.xpath("//span[@class='exclude-syndicated-hint hint--top']));
String toolTipText = toolTipElement.getText();
System.out.println("tooltip :- "+toolTipText);

答案 2 :(得分:0)

好,部分:

@FindBy(xpath = "//i[@class='syndicated-hint-icon fa fa-info-circle']") 
public WebElement tooltip; 

正在找到

<i class="syndicated-hint-icon fa fa-info-circle"></i>   

没有属性“数据提示”。 但是您仍然尝试从中获取属性。

String actualTooltip = tooltip.getAttribute("data-hint"); 

首先,如果要获取此值,甚至不需要将其悬停,将元素本身悬停并获取属性值就不是真正的“测试(此处未使用合适的词)”工具提示的东西。

但是从您仍然希望将其悬停的角度考虑,您可以这样做:

@FindBy(xpath = "LOCATOR FOR THE <span> AND NOT THE <i>") 
public WebElement tooltip;

public String getTooltipMessage() {
    Actions action = new Actions(driver); 
    action.moveToElement(tooltip).build().perform(); 
    String actualTooltip = tooltip.getAttribute("data-hint"); 
}

您只想获取消息的情况

@FindBy(xpath = "LOCATOR FOR THE <span> AND NOT THE <i>") 
public WebElement tooltip;

public String getTooltipMessage() {
    String actualTooltip = tooltip.getAttribute("data-hint"); 
}

答案 3 :(得分:0)

在CSS选择器下方尝试

@FindBy(css = "span[class='exclude-syndicated-hint.hint--top']");

 @FindBy(css = "i[class='syndicated-hint-icon.fa.fa-info-circle']");