'Select element by containing text in TestCafe

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?



Solution 1:[1]

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".

Solution 2:[2]

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']"));

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mate Mrše
Solution 2 vlatko606
Solution 3 Sarvesh