最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Testcafe get text from element - Stack Overflow

programmeradmin0浏览0评论

I'm trying to get a text from a modal on Chrome. Using the console, I can get the inner text as follows:

document.querySelector('.my-form > a').innerText
// returns 

Now, on my test, I can evaluate the element using

const myText = Selector('.my-form > a').innerText;
await t
  .expect(myText).contains('url');

and I can even click on that URL

await t.click(myText);

but I cannot put that inner text to a variable, for instance. I tried using a ClientFunction from this post

const getUrl = ClientFunction(() => document.querySelector('.my-form > a').innerText);

test('My Test', async t => {
const text = await getUrl();
console.log(text);
});

// this results in 
// TypeError: Cannot read property 'innerText' of null

and tried using a plain Selector as this post suggests

const text = Selector('.my-form > a').innerText;
const inner = await text.textContent;
console.log(inner);

// prints: undefined

How to extract a text from an element? I understand that t.selectText is limited in this scenario, right?

I'm trying to get a text from a modal on Chrome. Using the console, I can get the inner text as follows:

document.querySelector('.my-form > a').innerText
// returns http://a-url.com

Now, on my test, I can evaluate the element using

const myText = Selector('.my-form > a').innerText;
await t
  .expect(myText).contains('url');

and I can even click on that URL

await t.click(myText);

but I cannot put that inner text to a variable, for instance. I tried using a ClientFunction from this post

const getUrl = ClientFunction(() => document.querySelector('.my-form > a').innerText);

test('My Test', async t => {
const text = await getUrl();
console.log(text);
});

// this results in 
// TypeError: Cannot read property 'innerText' of null

and tried using a plain Selector as this post suggests

const text = Selector('.my-form > a').innerText;
const inner = await text.textContent;
console.log(inner);

// prints: undefined

How to extract a text from an element? I understand that t.selectText is limited in this scenario, right?

Share Improve this question edited Mar 25, 2019 at 8:59 Alex Skorkin 4,2743 gold badges26 silver badges48 bronze badges asked Mar 21, 2019 at 15:30 thenewjamesthenewjames 1,1643 gold badges15 silver badges29 bronze badges 2
  • 2 You shouldn't need a client function, Your last example is incorrect, did you try: const text = await Selector('.my-form >a').innerText; ? Note if there are multiple a tags in your form you will get all the text – ioseph Commented Mar 22, 2019 at 3:12
  • It works! @ioseph Do you want to answer so I can accept your answer? – thenewjames Commented Mar 22, 2019 at 13:21
Add a comment  | 

2 Answers 2

Reset to default 12

From the documentation you want:

const text = await Selector('.my-form > a').innerText;

That will do the trick:

const paragraph      = Selector("p").withText("possible entries");
const extractEntries = await paragraph.textContent;
发布评论

评论列表(0)

  1. 暂无评论