例如,给定链接的文本,检索孔链接元素。
我试过这个:
$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?
发布评论
评论(2)
你应该使用:
这是使用 CSS 选择器,它就像魔术一样!
但请确保“Italiano”引用页面上的元素(如果它是 ID,我会使用“#Italiano”)。
您可以在此处阅读遍历页面文档。
You should use:
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.
不要使用内容选择器。因为几乎在任何情况下它都会提供您意想不到的结果。例如,当您执行
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: