简体   繁体   English

如何使用Java验证Selenium WebDriver中的tooltipText

[英]How to verify tooltipText in Selenium WebDriver using Java

I have the following snippet of HTML code: 我有以下HTML代码片段:

<div style="float:right; padding-right: 50px; margin-top: -10px;" id="divTooltips">
<img width="25px" height="25px" src="/mkteditor/css/images/tooltip.png" alt="">
</div>

During mouse over tooltip.png a tooltip text "help text at top place" is displayed. 在鼠标悬停tooltip.png期间,将显示工具提示文本“帮助文本在顶部位置”。 I want to verify the tooltipText in WebDriver. 我想验证WebDriver中的tooltipText。 How to do it? 怎么做?

Whenever you will have tooltip then "title" attribute will be there. 每当你有工具提示时,那么“title”属性就在那里。 (as per my observation ). (根据我的观察)。

for example: Goto http://www.linkedin.com/ and view HTML code for LinkedIn image on top. 例如:转到http://www.linkedin.com/并在顶部查看LinkedIn图像的HTML代码。 LinkedIn LinkedIn

    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://www.linkedin.com/");
    WebElement onElement = driver.findElement(By.xpath("html/body/div[1]/div[1]/div/h2/a"));
    System.out.println("Tooltip : " + onElement.getAttribute("title"));

My observation says, whenever you focus on element and a tool-tip appears it is displayed inside a "div" which is made visible on mouse focus. 我的观察结果表明,每当你专注于元素并且出现一个工具提示时,它就显示在一个“div”中,该div在鼠标焦点上可见。

Here is something i would suggest, 这是我建议的,

  1. Move to element using actions class 使用actions类移动到元素

    Actions action = new Actions(driver); 动作动作=新动作(驱动程序); action.moveToElement('element_to_be_focused').build().perform(); 。action.moveToElement( 'element_to_be_focused')建立()执行();

  2. Now since tooltip displays some text, read that text and find out element from page source HTML where it is mentioned. 现在,由于工具提示显示了一些文本,因此请阅读该文本并从页面源HTML中找出提到它的元素。

  3. Now simply write code to wait for visibility of element having tooltip text. 现在只需编写代码以等待具有工具提示文本的元素的可见性。

    new WebDriverWait(driver, timeOutInSeconds).until(ExpectedConditions.visibilityOfElementLocated('element_locator')); new WebDriverWait(driver,timeOutInSeconds).until(ExpectedConditions.visibilityOfElementLocated('element_locator'));

  4. Get text from tooltip element using getText() 使用getText()从tooltip元素获取文本

To give an example, Click here 举个例子,点击这里

On given website once we mouse hover to textbox asking for age, we can see tooltip saying 'We ask for your age only for statistical purposes.' 在给定的网站上,一旦我们将鼠标悬停在文本框中询问年龄,我们可以看到工具提示“我们仅为统计目的询问您的年龄”。

Following is code snippet to get tooltip text: 以下是获取工具提示文本的代码段:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://jqueryui.com/tooltip/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
new Actions(driver).moveToElement(driver.findElement(By.xpath("//input[@id='age']"))).build().perform();
boolean isToolTipDisplayed = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).isDisplayed();
System.out.println("Is Tooltip displayed ? : " + isToolTipDisplayed);
            if (isToolTipDisplayed) {
                String tooltipText = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).getText();
System.out.println("Tooltip Text:- " + tooltipText);
}
driver.switchTo().defaultContent();
driver.quit();

Hope this will help you ! 希望对你有帮助 !

you can use this 你可以用它

WebElement element = driver.FindElement(By.id("divTooltips");
 string titleText = element.getAttribute("title");

now the title is stored in a titleText string.In case tooltip is hidden and you want to see hidden text on it check this 现在标题存储在titleText字符串中。如果工具提示被隐藏,你想看到它上面的隐藏文字检查这个

Enjoy! 请享用!

You can use the following to verify the tooltip text: 您可以使用以下内容验证工具提示文本:

try{
    String tooltipText = "help text at top place";
    String tooltipTextToVerify = driver.findElement(By.id("divTooltips").getAttribute("title");
      if (tooltipText.equels(tooltipTextToVerify)){
         return true;
      else {return false;}
}

You can try the following: 您可以尝试以下方法:

WebElement tooltipControl = driver.FindElement(By.id("divTooltips");
string titleText = tooltipControl.GetAttribute("textContent");

textContent attribute will provide the Text displayed in the tooltip. textContent属性将提供工具提示中显示的文本。

Hope this will help you. 希望这会帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在网页上使用带有 Java 的 Selenium WebDriver 验证工具提示文本 - How to verify tooltip text using Selenium WebDriver with java on a webpage 如果对象在Selenium Webdriver Java中不可见,如何使用xpath验证对象 - how to verify object using xpath if object is invisible in selenium webdriver java 如何使用Java使用Selenium WebDriver验证是否单击了按钮 - How to verify whether a Button is clicked or not with Selenium WebDriver using Java 如何使用Java在Selenium Webdriver中验证单词是否为粗体? - How to verify that a word is Bold in selenium webdriver using Java? 使用Java在Selenium WebDriver中发布后,每次如何验证文本? - How to verify text everytime after posting with Selenium WebDriver using java? 如何使用Java使用Selenium WebDriver验证动态库? - How to verify dynamic gallery with Selenium WebDriver using Java? 如何在Selenium Java Webdriver中验证图像的尺寸? - How to verify dimensions of image in Selenium Java Webdriver? 如何验证特定文本下的文本 <DIV> 使用Selenium WebDriver(java)和Chrome浏览器标记? - How can I verify presence of text under a specific <DIV> tag using Selenium WebDriver (java) and Chrome? 如何使用Java验证与Selenium WebDriver中的UI选项匹配的下拉选项? - How to verify the dropdown options matching with the UI options in selenium WebDriver using Java? 如何在Selenium Webdriver中使用Java验证HTML代码中是否存在用于下拉列表的标记 - How to verify whether a tag is present in the html code for a drop down using java in Selenium Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM