尝试对页脚中的项目使用“Then I press”(对于按钮)或“Then I follow”(对于链接)正则表达式失败,并出现错误“未找到具有 id|link|name 的元素/链接.. “作为一个例子,我在这个公共(public)网站上发现了这个异常:earthdata.nasa.gov 网站(我们的网站尚未激活)。屏幕左侧显示“反馈”的按钮不可点击(原文如此)。
我正在针对 selenium 2.29.0 服务器运行测试。我如何扩展 Mink 以便它可以定位并“单击”链接,例如上面提到的“反馈”按钮?
除了使用 id 来点击链接,您还可以使用 XPath 来点击链接。 XPath 是一种在 DOM 中识别对象的通用方法,因此它始终有效。
对于那些没有使用过 Behat 的人,请使用此链接阅读更多信息。它基本上是 Selenium 工具的包装器。
http://mink.behat.org/
并使用它来访问 Selenium 网络自动化测试站点:
http://docs.seleniumhq.org/download/
编辑:
Ian:感谢 MacGyver 的指点,这里是解决方案:
/** Click on the element with the provided xpath query
*
* @When /^I click on the element with xpath "([^"]*)"$/
*/
public function iClickOnTheElementWithXPath($xpath)
{
$session = $this->getSession(); // get the mink session
$element = $session->getPage()->find(
'xpath',
$session->getSelectorsHandler()->selectorToXpath('xpath', $xpath)
); // runs the actual query and returns the element
// errors must not pass silently
if (null === $element) {
throw new \InvalidArgumentException(sprintf('Could not evaluate XPath: "%s"', $xpath));
}
// ok, let's click on it
$element->click();
}