简体   繁体   English

如何从 TestCafe 选择器中提取底层 HTML dom 元素?

[英]How do I extract the underlying HTML dom element from a TestCafe Selector?

I'm trying out TestCafe and having problems locating some of my elements.我正在尝试 TestCafe,但在定位我的一些元素时遇到了问题。 I want to be able to, for debugging purposes, print out whatever element(s) that TestCafe has found with a particular Selector so I can compare them against the DOM that exists.出于调试目的,我希望能够打印出 TestCafe 使用特定选择器找到的任何元素,以便我可以将它们与存在的 DOM 进行比较。

I have already tried reading the documentation and running this code:我已经尝试阅读文档并运行此代码:

const a = await Selector('span', { timeout: 20000 }).withText('some text');
console.log(a);

However, when I run this code, all I get is a printout of some internal TestCafe type like so:但是,当我运行此代码时,我得到的只是一些内部 TestCafe 类型的打印输出,如下所示:

[Function: __$$clientFunction$$] {
  with: [Function],
  nth: [Function],
...
}

For anyone who wants the answer for later, to get the actual html element, you need to call the Selector, ie:对于以后想要答案的任何人,要获得实际的 html 元素,您需要调用选择器,即:

const a = await Selector('span', { timeout: 20000 }).withText('text'))()
console.log(a)

This is because Selector creates a function that testcafe uses and can execute internally to get elements, but if you want to manually do that, you need to call it.这是因为Selector创建了一个 function 供 testcafe 使用,并且可以在内部执行以获取元素,但是如果您想手动执行此操作,则需要调用它。

example output示例 output

{
  nodeType: 1,
  textContent: 'Before or on',
  childNodeCount: 1,
  hasChildNodes: true,
  childElementCount: 1,
  hasChildElements: true,
  tagName: 'span',
  visible: true,
  focused: false,
  attributes: {
    'data-role': 'filtercell',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM