如何找到元素

时间:2014-11-06 16:03:46

标签: java selenium selenium-webdriver webdriver

我想在html文件中的<p>标签中找到元素。 html的这一部分在网页中显示警告消息。

html代码在这里:

<div class="Coeff-Details">
    <div>
        <p class="alert alert-warning">There is no data to display</p>
    </div>
</div>

我在课后找不到这个元素,我试过这个没有成功。

driver.findElement(By.name("alert alert-warning">There is no data to display")).isDisplayed();

我尝试了同样的事情,但是使用xpath,它没有找到元素。

你知道如何找到这个元素吗? 谢谢

1 个答案:

答案 0 :(得分:1)

有多种方法可以找到元素,以下是其中一些:

  • 班级名称

    WebElement element = driver.findElement(By.className("alert-warning"));
    
  • xpath

  • WebElement element = driver.findElement(By.xpath('//div[@class="Coeff-Details"]/div/p[@class="alert-warning"]'));
    
  • css选择

  • WebElement element = driver.findElement(By.cssSelector('div.Coeff-Details p.alert.alert-warning'));
    

然后,您可以使用getText()方法获取文本:

element.getText();