【问题标题】:Selenium: Getting ToolTip of an element?Selenium:获取元素的工具提示?
【发布时间】:2015-07-20 04:15:29
【问题描述】:

我正在尝试从站点中的元素中提取文本。 HTML 代码是这样的:

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

下面是我的TestNG 代码:

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();
  }

}

【问题讨论】:

  • 请说明您到目前为止所尝试的内容以及您遇到的错误?
  • 当我尝试执行上面的代码时,它的输出为“null”,我想打印这个文本“给它目的——用书籍、电影、手机、相机、玩具和时尚填充它首饰。”当鼠标移动到网页元素时。
  • 如果要获取

    标签中包含的文本,只需使用 elem.getText();这行得通吗?以下是如何处理元素的示例,也许这对您有帮助 stackoverflow.com/questions/17293914/…

  • elem.getText(); dint 工作,我尝试过但无法获得输出。
  • 也许您应该验证您的选择器是否按预期工作。你在萤火虫或类似的东西中试过吗?

标签: java selenium selenium-webdriver


【解决方案1】:

工具提示大多是硬编码的,如果是这种情况,您(可能)唯一想做的就是测试它是否显示。据我所知,以下将是最好的解决方案。

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
    {

    }
}

【讨论】:

猜你喜欢
  • 2011-01-15
  • 2021-10-15
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多