1

I am trying to extract the text from an element in the site. The HTML code is something like this:

<p class= "nav_p">
" Give it purpose—fill it with books, movies, mobiles, cameras, toys and fashion jewellery."
</p>

Below is my TestNG code:

package TestNG;    
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class Tooltip {


    WebDriver driver;
    WebDriverWait wait;
    By stayOn=By.cssSelector("span[class='redir-a-button-sec-center']");
    By cart=By.cssSelector("span[class='nav-line-2']");


  @Test
  public void cart() throws Exception {
      driver.navigate().to("http://amazon.com");
      driver.manage().window().maximize();
      new WebDriverWait(driver, 5);
      driver.findElement(stayOn).click();


 Actions builder=new Actions(driver);

 WebElement elem=driver.findElement(By.xpath(".//*[@id='nav-cart']"));

 Action action = builder.moveToElement(elem).build();
 action.perform();
 Thread.sleep(2000);


WebElement elem1=driver.findElement(By.cssSelector("p[class='nav_p ']"));
String str=elem1.getAttribute("paragraph");

System.out.println(str);

  }
  @BeforeTest
  public void beforeTest() {

      System.setProperty("webdriver.chrome.driver", "E:\\selenium\\lib\\chromedriver_win32\\chromedriver.exe");
      driver=new ChromeDriver();

  }

  @AfterTest
  public void afterTest() {

      driver.quit();
  }

}
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • pls state what you have tried so far and what error do you get? – Dude May 09 '15 at 08:07
  • when i try to execute the above code, its giving output as "null" and i want to print this text "Give it purpose—fill it with books, movies, mobiles, cameras, toys and fashion jewellery." when the mouse is moved to web element. – rahul bommanaboina May 09 '15 at 11:00
  • 1
    if you want to get the text included in the

    tag, just use elem.getText(); does this work? here are exampes on how to hower an element, maybe this helps you http://stackoverflow.com/questions/17293914/how-to-perform-mouseover-function-in-selenium-webdriver-using-java

    – Dude May 09 '15 at 11:08
  • elem.getText(); dint worked , i tried it but not able to get the output. – rahul bommanaboina May 10 '15 at 08:55
  • maybe you should validate that your selector is working as expected. have you tried it in firebug or something like that? – Dude May 12 '15 at 07:16

1 Answers1

2

Tooltips are mostly hardcoded and if that's case the only thing you(probably) want to do is to test if that's displayed or not. The following would be the best solution according to my knowledge.

public void TestToolTip()
{
    //Assuming that's the only element with that class name
    //If that's not case adjust the selector accordingly
    //you also have to make sure the element exist as well
    //otherwise it will throw NoSuchElement(or similar) exception
    By by = By.cssSelector("p.nav_p");

    WebElement toolTipElement = driver.findElement(by);
    //the following action should make the tooltip visible
    //if this is not the element on which the click
    //event generates tool to visible, adjust accordingly.
    toolTipElement.click();


    //In this stage just check if the element is visible/displayed or not
    if (!toolTipElement.isDisplayed() || !toolTipElement.getText().contains("Give it purpose—fill it with books, movies, mobiles, cameras, toys and fashion jewellery."))
    {
        //is not displayed or does not contain the expected text so fail the test
        Assert.fail();
    }
     else
    {

    }
}
Saifur
  • 16,081
  • 6
  • 49
  • 73