Select element by containing text in TestCafe

Viewed 1674

How can I select an HTML element containing specific text?

In Selenium Xpath selectors are used, but TestCafe doesn't support Xpath.

How do I do it in TestCafe?

3 Answers

According to the official documentation, in order to select an element containing a certain text, I should use the .withText() method with the selected element, example given is:

import { Selector } from 'testcafe';

fixture `Example`
    .page `https://devexpress.github.io/testcafe/example/`;

test('Click test', async t => {
    const selectBasedOnText = Selector('label').withText('I have tried TestCafe');

    await t
        .click(selectBasedOnText);
});

for selecting a label element with the text "I have tried TestCafe".

what worked for me:

import xPathToCss from 'xpath-to-css'

and then use as ex:

.click(xPathToCss("//a[@class='icon-menu login show-login-panel list-item list-item-next']"));

no, testcafe supports xpath

https://github.com/Programmingarea/XpathSelectorIhaveNotCreated

download the xpath selector after downloading that go to your testcafe file and type:

importing xpath-selector

import XPathSelector from './xpath-selector';

using it:

const usingxpath = XPathSelector("your xpath");

simple! if any doubt ask it in reply

Related