开发者

Mink selectors: any way to get the element in base of the content inside?

开发者 https://www.devze.com 2023-04-09 10:10 出处:网络
for example, given the text of a link, retrieve the hole link element. I tried this: $page = $this->getSession()->getPage();

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_getBrowserS开发者_JAVA百科cript?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?


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.


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:

  • http://mink.behat.org
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号