简体   繁体   English

使用Selenium / webdriver声明元标记(java)

[英]Asserting Meta Tags using Selenium/webdriver (java)

I have the following piece of html: 我有以下的html片段:

<head>
<meta name="twitter:card" content="summary"></meta>
</head>

Using Selenium Webdriver (Cucumber) I've the following piece of code which doesn't seem to be working: 使用Selenium Webdriver(Cucumber),我有以下似乎不起作用的代码:

    @and("^Meta is being checked with property \"([^\"]*)\"$")
    public void check_meta(String property) throws Throwable {
    String meta = driver.findElement(By.xpath("//meta[contains(text(), \"" + property + "\")]")).getText();
    Assert.assertTrue(meta.contains(property));
}

I'm trying to assert if twitter:card is present, but it gives me assertion errors now. 我正在尝试断言twitter:card是否存在,但是现在它给了我断言错误。 What am I doing wrong? 我究竟做错了什么? What is the correct definition? 正确的定义是什么?

Your xpath is checking the text() of the meta tag. 您的xpath正在检查meta标签的text() The text() will return what's in the block. text()将返回块中的内容。

<meta ....></meta>
          /\  It will return what's in here.

What YOU want, is to validate the meta content attribute. 您想要的是验证meta content属性。

Try: 尝试:

Assert.assertTrue(
    driver.findElements(By.cssSelector("meta[name='twitter\\:card']")).size() > 0
);

this will validate that the <meta name="twitter:card" ... /> is there, but not checking the content. 这将验证<meta name="twitter:card" ... />是否存在,但不检查内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM