我想验证我网页上的新按钮上的工具提示文本。鼠标悬停前NEW Button的html后端代码如下:
<button id="newBtn" class="checkForSave" title="Create a new Configuration Package" type="button">New</button>
鼠标悬停在新按钮上后,新按钮的html后端代码如下:
<button id="newBtn" class="checkForSave" title="" type="button"> area-describdby="ui-tooltip-27"New</button>
现在,我想验证鼠标悬停在new按钮上后,工具提示文本“Create a new Configuration Package”是否应该出现。
通过下面的代码,我可以在鼠标悬停之前将文本存储在字符串中:
WebElement NewButton = driver.findElement(By.id("newBtn"));
String Tooltip = NewButton.getAttribute("title");
System.out.println(Tooltip);
其打印文本“创建新的配置包”
但当我将鼠标移到NEW按钮上并编写代码时
Actions ActionChains = new Actions(driver);
WebElement NewButton = driver.findElement(By.id("newBtn"));
actions.moveToElement(NewButton).build().perform();
ActionChains.clickAndHold(NewButton).perform();
String Tooltip = NewButton.getAttribute("title");
System.out.println(Tooltip);
我没有得到任何东西,我得到空白消息,因为鼠标悬停后,新按钮的后端html代码中的标题是“”。
我该怎么做?
注释掉action chains . click and hold(new button)行。执行();
import org.openqa.selenium.interactions.HasInputDevices;
import org.openqa.selenium.interactions.Mouse;
import org.openqa.selenium.internal.Locatable;
Locatable hoverItem = (Locatable) webEleObj;
Mouse mouse = ((HasInputDevices) getDriver()).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());
尝试使用 xpath 查找元素!它可能会像我尝试使用“id”一样对您有所帮助