java - Selenium:获取元素的工具提示?

标签 java selenium selenium-webdriver

我正在尝试从网站中的元素中提取文本。 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();
  }

}

最佳答案

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

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
    {

    }
}

关于java - Selenium:获取元素的工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30137514/

相关文章:

java - Swing : Setting JButton's background

Selenium Webdriver - 在 If 语句中使用 isDisplayed() 不起作用

java - 如何保存完整的网页

java - 想要在 java selenium 中打印 Instagram 帖子的标题

python - 为什么我的程序返回 "No review v1"而不是返回有评论的产品的平均评论?(网络抓取,python)

java - 错误: Caught exception [ERROR: Unsupported command [selectFrame | index=1 | ]] while exporting code from Selenium IDE to Webdriver

Java组播发送者+接收者

java - Apache poi如何获取单元格坐标

java - CustomTaskChange 在调用 updateSQL 时实际执行

selenium - Webdriver (c#) - 可能通过文本查找按钮