通过 XPath Sales Navigator 访问元素列表中的子项

Posted

技术标签:

【中文标题】通过 XPath Sales Navigator 访问元素列表中的子项【英文标题】:Access child inside an element list by XPath Sales Navigator 【发布时间】:2021-03-04 17:09:12 【问题描述】:

我正在尝试从 Sales Navigator 中提取随机人员的姓名和职位。每个人都显示为一张包含所有信息的卡片。我获得了一张卡片列表,然后我想为每一张卡片获取名称和标题。我尝试使用下面的代码从卡片中获取信息,一个结果的 html 如下。

到目前为止,我的尝试总是返回一个错误,表明找不到该元素。我该如何解决这个问题?

def testeo(driver):
    lista = driver.find_elements_by_xpath("//*[contains(@class,'pv5 ph2 search-results__result-item')]")
    nombres = []
    for i in range(0, len(lista)):
        nombres.append((lista[i].find_element_by_xpath(".//*[contains(@class,'result-lockup__name')]").text,
                       lista[i].find_element_by_xpath(".//*[contains(@class,'t-14 t-bold')]").text))
<li class="pv5 ph2 search-results__result-item" data-scroll-into-view="urn:li:fs_salesProfile:(ACwAAAJ-Ab0Bu4JpScPs9SE2b8R_LP9L9vU9nM8,NAME_SEARCH,fH_T)">
      <div class="pt5 absolute search-results__select-container">
    <input id="search-result-ember6830" class="small-input ember-checkbox ember-view" type="checkbox">
    <label class="m0" for="search-result-ember6830">
      <span class="a11y-text">
          Select Jean Jongejan
      </span>
    </label>
</div>

<div style="" id="ember6866" class="flex full-width deferred-area ember-view">    <div class="search-results__result-container full-width pl2">
<div id="ember6981" class="ember-view">        <div id="ember6982" class="ember-view">

  <article>
    <h3 class="a11y-text">
      Profile result – Jean Jongejan
    </h3>

    <section class="result-lockup">
      <h4 class="a11y-text">
        Profile result lockup – Jean Jongejan
      </h4>
      <div class="result-lockup__profile-info flex flex-column">
        

<div class="horizontal-person-entity-lockup-4 result-lockup__entity ml6">
  <figure>
<a href="/sales/people/ACwAAAJ-Ab0Bu4JpScPs9SE2b8R_LP9L9vU9nM8,NAME_SEARCH,fH_T?_ntb=ErSmZYlWS8KlI9CD0cB6Yg%3D%3D" id="ember6985" class="result-lockup__icon-link ember-view">      
<div class="presence-entity--size-4 relative mr2">
  <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" loading="lazy"  id="ember6986" class="max-width max-height circle-entity-4 lazy-image ghost-person loaded ember-view">

          

<div class="presence-indicator presence-indicator--size-4 hidden presence-entity__indicator presence-entity__indicator--size-4" title="Reachable">
  <span class="a11y-text">
    Jean Jongejan is reachable
  </span>
</div>

    
</div>

</a>  </figure>
  <dl>
    <dt class="result-lockup__name">
<a href="/sales/people/ACwAAAJ-Ab0Bu4JpScPs9SE2b8R_LP9L9vU9nM8,NAME_SEARCH,fH_T?_ntb=ErSmZYlWS8KlI9CD0cB6Yg%3D%3D" id="ember6989" class="ember-view">        Jean Jongejan
</a>    </dt>
    <dd class="inline-flex vertical-align-middle">
      <ul class="ml1 flex align-items-center list-style-none">
          <li class="mr1">
            <span class="a11y-text">
              3rd degree contact
            </span>
            <span class="label-16dp block" aria-hidden="true">
              3rd
            </span>
          </li>
<!----><!----><!---->      </ul>
    </dd>
      <dd class="result-lockup__highlight-keyword">
          <span class="t-14 t-bold">EXT Key Account Management &amp; Consultancy</span>
            <span>
    at
    <span data-entity-hovercard-id="urn:li:fs_salesCompany:36314" class="result-lockup__position-company">
<a href="/sales/company/36314?_ntb=Z6Rvdg6sRMiPD6xsYlUuFQ%3D%3D" id="ember6991" class="Sans-14px-black-75%-bold ember-view">        <span aria-hidden="true">
          Marimekko
        </span>
        <span class="a11y-text">
          Go to Marimekko account page
        </span>
</a>      <button aria-expanded="false" aria-label="See more about Marimekko" class="entity-hovercard__a11y-trigger p0 b0" data-entity-hovercard-id="urn:li:fs_salesCompany:36314" data-entity-hovercard-trigger="click"></button>
    </span>
  </span>

      </dd>
      <dd>
          <span class="t-12 t-black--light">
            3 years 11 months in role and company
          </span>
      </dd>
      <dd>
        <ul class="mv1 t-12 t-black--light result-lockup__misc-list">
            <li class="result-lockup__misc-item">Breda, North Brabant, Netherlands</li>
        </ul>
      </dd>
  </dl>
</div>


<!---->      </div>

      

<div class="result-lockup__actions flex">
  <ul class="result-lockup__common-actions">
      <li class="result-lockup__action-item mb3">
        <div class="display-flex">
<div id="ember6993" class="ember-view">                    <div id="ember6995" class="save-to-list-dropdown  artdeco-dropdown artdeco-dropdown--placement-bottom artdeco-dropdown--justification-right ember-view"><button aria-expanded="false" id="ember6996" class="save-to-list-dropdown__trigger ph4 artdeco-button artdeco-button--secondary artdeco-button--pro artdeco-button--1 m-type--message artdeco-dropdown__trigger artdeco-dropdown__trigger--placement-bottom ember-view" type="button" tabindex="0">    Save

<!----></button><div tabindex="-1" aria-hidden="true" id="ember6997" class="save-to-list-dropdown__content-container artdeco-dropdown__content artdeco-dropdown--is-dropdown-element artdeco-dropdown__content--has-arrow artdeco-dropdown__content--arrow-right artdeco-dropdown__content--justification-right artdeco-dropdown__content--placement-bottom ember-view"><div class="artdeco-dropdown__content-inner">
  <!---->
</div>
</div></div>

<div id="ember6998" class="ember-view">
<!---->
<!----></div>
</div>                      <div class="relative">
<div id="ember6999" class="ember-view">
<div id="ember7000" class="artdeco-dropdown artdeco-dropdown--placement-bottom artdeco-dropdown--justification-right ember-view"><button aria-expanded="false" id="ember7001" class="artdeco-dropdown__trigger result-lockup__action-button m-type--more artdeco-dropdown__trigger--non-button artdeco-dropdown__trigger--placement-bottom ember-view" type="button" tabindex="0">                  <span class="a11y-text">See more actions for this result</span>
                  <li-icon aria-hidden="true" type="ellipsis-horizontal-icon" class="artdeco-button artdeco-button--tertiary artdeco-button--1 artdeco-button--muted p0" size="medium"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" data-supported-dps="24x24" fill="currentColor"   focusable="false">
  <path d="M2 10h4v4H2v-4zm8 4h4v-4h-4v4zm8-4v4h4v-4h-4z"></path>
</svg></li-icon>

<!----></button><div tabindex="-1" aria-hidden="true" id="ember7002" class="artdeco-dropdown__content result-lockup__dropdown-more artdeco-dropdown--is-dropdown-element artdeco-dropdown__content--has-arrow artdeco-dropdown__content--arrow-right artdeco-dropdown__content--justification-right artdeco-dropdown__content--placement-bottom ember-view"><!----></div></div>

<div id="ember7003" class="ember-view"><!----></div>
<!----></div>          </div>
        </div>
      </li>
<!---->  </ul>
</div>

    </section>


      

  <section class="result-context relative pt1">
    <h4 class="a11y-text">Profile result context – Jean Jongejan</h4>

<!---->
<!---->
<!---->  </section>

  </article>
</div>

</div>  </div>

</div>
    </li>

【问题讨论】:

请编辑您的问题并确保您的 html 有效。 嗨,杰克!有效是指它是完整的吗?还是格式正确?我将重新粘贴该卡的完整 HTML 【参考方案1】:

你可以试试这个吗?

//*[name()='dt'][@class='result-lockup__name']

【讨论】:

我已经使用 child: ".//child::*[contains(@class,'result-lockup__name')]" 解决了这个问题,现在它就像一个魅力!

以上是关于通过 XPath Sales Navigator 访问元素列表中的子项的主要内容,如果未能解决你的问题,请参考以下文章

现在还支持navigator.geolocation定位吗?

xpath学习,通过xpath 采集数据

通过xpath相对节点位置查找元素

javascript通过navigator.userAgent识别各种浏览器

selenium:Xpath定位详解

自动化定位——通过XPath定位元素