代码之家  ›  专栏  ›  技术社区  ›  Dave Peck

木偶演员ElementHandle的预期行为是什么。getProperty()?

  •  7
  • Dave Peck  · 技术社区  · 7 年前

    木偶师1.0.0-立柱。这个 getProperty() 这种方法似乎有点神奇。例如,如果您的页面包含:

    <a href="/foo/bar.html">link</a>
    

    那么这将不会返回 相对的 但是 完全的 URL:

    const propertyHandle = await elementHandle.getProperty('href');
    const href = await propertyHandle.jsonValue();
    // href is 'https://localhost:8080/foo/bar.html'
    

    另一方面,如果你要做更多的迂回:

    const hrefHandle = await page.evaluateHandle(element => element.getAttribute('href'), elementHandle);
    const href = await hrefHandle.jsonValue();
    // href is '/foo/bar.html'
    

    据我所知,木偶师文档中没有提到 getProperty() ?

    例如,如果你想得到 style 元素的属性。看起来像是木偶师的 getProperty() 实际上,尝试以某种方式解析样式,这种解析有缺陷/不完整。获取原始文本的唯一方法是使用 evaluateHandle(...) .

    这是一个预期的功能,只是一个文档错误吗?还是仅仅是一只傀儡虫子?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  4
  •   Pasi    7 年前

    看见 HTML - attributes vs properties 了解HTML属性和DOM属性之间的差异。

    你也可以很容易地看到没有木偶演员的区别。例如,在此页面上:

    document.getElementById('nav-questions').href    
    // returns "https://stackoverflow.com/questions"
    
    document.getElementById('nav-questions').getAttribute('href')    
    // returns "/questions"