【问题标题】:Extract a Value from the DOM从 DOM 中提取值
【发布时间】:2019-05-29 19:06:32
【问题描述】:

Q1) 如何使用 FindElement/By 类到达 XML/HTML 中的“1”?

<h3 class="lemon--h3__373c0__sQmiG heading--h3__373c0__1n4Of alternate__373c0__1uacp">1<!-- -->.&nbsp;<a class="lemon--a__373c0__IEZFH link__373c0__29943 link-color--blue-dark__373c0__1mhJo link-size--inherit__373c0__2JXk5" href="/biz/teds-montana-grill-larimer-square-denver?osq=Teds+Montana+Grill" target="" name="Ted’s Montana Grill - Larimer Square" rel="">Ted’s Montana Grill - Larimer Square</a></h3>

Q2) 如何从下面的 xml 中获取 Ted's Montana GrillAurora? 我设法得到Ted’s Montana Grill。然而,Aurora 被证明具有挑战性。

这是网址 - https://www.yelp.com/biz/teds-montana-grill-aurora-aurora?osq=Teds+Montana+Grill

<h1 class="biz-page-title embossed-text-white">Ted’s Montana Grill -</h1>
<div class="u-inline-block">
            <h1 class="biz-page-title embossed-text-white">Aurora</h1>
            <div class="u-inline-block">

尝试使用 FindElement/By

【问题讨论】:

    标签: html getelementsbyclassname inspect


    【解决方案1】:

    第一季度:

    document.getElementsByClassName("lemon--h3__373c0__sQmiG heading--h3__373c0__1n4Of alternate__373c0__1uacp");
    

    第二季度:

    let items = document.getElementsByClassName("biz-page-title embossed-text-white");
    for (let i = 0; i < items.length; i++) {
        console.log(items[i].outerText);
    }
    

    【讨论】:

      【解决方案2】:

      2) 它们都具有相同的类,因此您可以遍历 getElementsByClassName 返回的所有节点。您需要做的就是访问textContent 属性。

      const titles = document.getElementsByClassName("biz-page-title");
      Array.prototype.forEach.call(titles, function(el) {
          console.log(el.textContent);
      })
      

      1) 第一个问题也一样:const value = document.getElementsByClassName("lemon--h3__373c0__sQmiG heading--h3__373c0__1n4Of alternate__373c0__1uacp")[0].textContent;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-16
        • 1970-01-01
        • 1970-01-01
        • 2020-12-06
        • 2011-12-11
        • 1970-01-01
        • 2016-08-10
        相关资源
        最近更新 更多