0
votes

I am trying to access an element using XPath in Behat and Mink context an element which has no but my function can't seem to find it

Here's the element structure:

 <a data-iskeynav="true" data-test-id="compose-button" href="/d/compose/4661075315" class="e_dRA l_T cn_dBP cg_FJ k_w r_P A_6EqO u_e69 p_R S_n C_52qC I_Z1UEcsX D_F H_6VdP gl_C ab_C en_0 M_1Eu7sD ir3_Z1FS2Mn it3_dRA" role="button" 
aria-label="Compose" tabindex="20">Compose</a>

And here's my php code

    $page = $this->getSession()->getPage();
    $element = $page->find('xpath', '//a[@data-test-id = "' . $id . '"]');

I even used this XPath, copied by the browser but with no success

//*[@id="app"]/div[1]/div/div[1]/nav/div/div[1]/a
2
We'll need more context - at least as much as the XPath suggests (e.g. all the divs leading to that a). - BoltClock

2 Answers

0
votes

You should try to add a condition to that a, that has a certain attribute with value or contains some value in the attribute

Examples:

css: #app a[data-test-id=compose-button]
css: #app a[href*=compose]

xpath: //*[@id='app']//a[@data-test-id='compose-button']
xpath: //*[@id='app']//a[contains(@href, 'compose')]

0
votes

Try with contains keyword:

It should be:

//a[contains(@data-test-id, '"'. $id . '"')]