Mink 选择器:有什么方法可以获取内部内容基础中的元素吗?

发布于 2024-12-07 14:24:55 字数 1957 浏览 2 评论 0 原文

例如,给定链接的文本,检索孔链接元素。

我试过这个:

$page = $this->getSession()->getPage();
$page->find('content', 'Italiano');

但它说:

选择器“内容”未注册。

编辑:检查everzet答案的链接后,我得到了这个:

$el = $page->find('named', array(
        'content', $this->getMainContext()->getSession()->getSelectorsHandler()->xpathLiteral('Pound')));

$el->click();

但是我收到了关于click()函数的错误:

当我选择“英镑”时#

MyFirm\FrontendBundle\Features\Context\CurrencyControllerContext::iChoose()

错误:_click(_byXPath("(//html/./descendant-or-self::*[包含(标准化空间(.), '英镑')])[1]")) 类型错误:parent.tagName 未定义 ([目的 HTMLHtmlElement],"A",1)@http://myfirm.localhost/s/spr/concat.js:3286 ([目的 HTMLHtmlElement],“A”)@http://myfirm.localhost/s/spr/concat.js:3762 ([目的 HTMLHtmlElement])@http://myfirm.localhost/s/spr/concat.js:331 ([对象 HTMLHtmlElement],false,false,(void 0))@http://myfirm.localhost/s/spr/concat.js:708 ([目的 HTMLHtmlElement])@http://myfirm.localhost/s/spr/concat.js:478 ()@http://myfirm.localhost/s/spr/concat.js:3016 ()@http://myfirm.localhost/s/spr/concat.js:3016 @http://myfirm.localhost/s/spr/concat.js:2822

<前><代码>

href='/s/dyn/Log_getBrowserScript?href=null&n=3286'>点击查看 浏览器脚本

这是var_dump($el)输出的开始:

object(Behat\Mink\Element\NodeElement)#438 (2) {
  ["xpath":"Behat\Mink\Element\NodeElement":private]=>
  string(74) "(//html/./descendant-or-self::*[contains(normalize-space(.), 'Pound')])[1]"

$el->getTagName()的输出是'html' 。

这是因为我试图单击不是元素而只是内容的东西吗?那么,有什么办法可以从内容中获取元素呢?

for example, given the text of a link, retrieve the hole link element.

I tried this:

$page = $this->getSession()->getPage();
$page->find('content', 'Italiano');

But it says:

Selector "content" is not registered.

EDIT: after check the links of the everzet's answers I have this:

$el = $page->find('named', array(
        'content', $this->getMainContext()->getSession()->getSelectorsHandler()->xpathLiteral('Pound')));

$el->click();

but I'm getting this error about click() function:

When I choose "Pound sterling" #

MyFirm\FrontendBundle\Features\Context\CurrencyControllerContext::iChoose()

error:_click(_byXPath("(//html/./descendant-or-self::*[contains(normalize-space(.),
'Pound')])[1]"))
TypeError: parent.tagName is undefined
([object
HTMLHtmlElement],"A",1)@http://myfirm.localhost/s/spr/concat.js:3286
([object
HTMLHtmlElement],"A")@http://myfirm.localhost/s/spr/concat.js:3762
([object
HTMLHtmlElement])@http://myfirm.localhost/s/spr/concat.js:331
([object HTMLHtmlElement],false,false,(void
0))@http://myfirm.localhost/s/spr/concat.js:708
([object
HTMLHtmlElement])@http://myfirm.localhost/s/spr/concat.js:478
()@http://myfirm.localhost/s/spr/concat.js:3016
()@http://myfirm.localhost/s/spr/concat.js:3016
@http://myfirm.localhost/s/spr/concat.js:2822

  <a

href='/s/dyn/Log_getBrowserScript?href=null&n=3286'>Click for
browser script

This is the beginning of the output of var_dump($el):

object(Behat\Mink\Element\NodeElement)#438 (2) {
  ["xpath":"Behat\Mink\Element\NodeElement":private]=>
  string(74) "(//html/./descendant-or-self::*[contains(normalize-space(.), 'Pound')])[1]"

And the output of $el->getTagName() is 'html'.

Is that because I'm trying to click something that is not an element but just content? In that case, any way to get the element from the content?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转瞬即逝 2024-12-14 14:24:55

你应该使用:

$page = $this->getSession()->getPage();
$page->find('css', 'Italiano');

这是使用 CSS 选择器,它就像魔术一样!
但请确保“Italiano”引用页面上的元素(如果它是 ID,我会使用“#Italiano”)。

您可以在此处阅读遍历页面文档。

You should use:

$page = $this->getSession()->getPage();
$page->find('css', 'Italiano');

This is using the CSS selectors, and it works like a magic!
But make sure 'Italiano' refers to an element on the page (I would use '#Italiano' if it's an ID).

You can read the traversing pages documentation here.

好听的两个字的网名 2024-12-14 14:24:55

不要使用内容选择器。因为几乎在任何情况下它都会提供您意想不到的结果。例如,当您执行 content(Pound) 时,您期望它返回一个包含此文本的元素。现实情况是,目标元素的每个父元素都包含“Pound”文本,包括 。这就是为什么最后会得到 ;-)

Minks 选择器在手册中得到了很好的解释:

Don't use content selector. Because in almost any case it will provide results, that you don't expect. For example, when you're doing content(Pound), you're expecting that it'll return you an element, that contains this text. The reality is, every parent element of target one is containing your "Pound" text, including <html/>. That's why you get <html/> at the end ;-)

Minks selectors are well-explained in the manual:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文