[英]How to handel tooltip in selenium java
工具提示圖片無法在 selenium java 中使用 getAttribute 和 getText 處理工具提示。 下面是代碼
HTML:
<span class="exclude-syndicated-hint hint--top" data-hint="Programs like
"The Today Show" 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>
爪哇:
@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");
}
// 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);
試試這個:-
您需要在Actions
類方法上調用perform()
。
首先,我們需要通過傳遞webdriver實例來創建新的action builder實例。
閱讀有關moveToElement的更多信息
有關鼠標懸停操作的更多信息,請閱讀此內容
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);
好的,部分:
@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");
}
試試下面的 css 選擇器
@FindBy(css = "span[class='exclude-syndicated-hint.hint--top']");
要么
@FindBy(css = "i[class='syndicated-hint-icon.fa.fa-info-circle']");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.