php - 如何在 behat selenium 中获取 Xpath

标签 php selenium xpath behat

试图在页面中查找 Xpath。
这是我的代码。

   $session = $this->getSession();
   $found = $session->getPage()->findAll('xpath', '//meta[@name ="description"]/@content');
   if (is_null($found)) {
      throw new \Exception(sprintf("Could not find meta %s='%s' with '%s'",));
   }

给出错误

invalid selector: The result of the xpath expression "//html//meta[@name ="description"]/@content" is: [object Attr]. It should be an element. (Session info: chrome=51.0.2704.103) (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.10.5 x86_64) (WARNING: The server did not provide any stacktrace information)

最佳答案

使用“@content”将返回属性内容,当 Behat 转换为 xpath 时,这可能不会返回有效的选择器。

如果您需要指定该属性,您应该像这样使用它:

//meta[@name ="description"][@content]

另请注意,findAll 返回数组而不是单个元素,您可以使用 find 查找与给定选择器匹配的第一个元素。

如果需要获取属性值或获取文本,可以使用 getAttribute('attribute_name') 或 getText() 等方法。

在你的情况下,你可以有类似的东西:
$found->getAttribute('name');

或者
$found->getText();

关于php - 如何在 behat selenium 中获取 Xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38480330/

相关文章:

php - 如何使用 php 将 %20 替换为 url 中的 - ?

php - Twig Loader 命名空间

java - 我想动态访问循环中的变量

java - JBoss5 中的 XPath 类解析

c# - 使用 HTML Agility Pack 查找多个表

php - 锁定/禁用/隐藏 WordPress 主题选项,直到用户注册新闻通讯

php - 从 MySql (ASP.NET) 读取西里尔字母

selenium - Protractor 5.1.1 selenium-webdriver 版本不一致

selenium - 如何将这个xpath转换为css?

xml - 如何使用 XQuery 更新 XML 变量中的属性值?