【问题标题】:Getting current designation using Python Selenium使用 Python Selenium 获取当前名称
【发布时间】:2021-08-24 10:56:51
【问题描述】:

我正在尝试使用 Python Selenium 获取 LinkedIn 个人资料列表的当前名称。我想要“当前:”和“摘要:”之后的部分。

这是 HTML:

<div class="linked-area flex-1 cursor-pointer">
  
        <p class="entity-result__summary entity-result__summary--2-lines t-12 t-black--light ">
          <!---->Current: Full Stack Software<span class="white-space-pre"> </span>
<strong><!---->Developer<!----></strong><span class="white-space-pre"> </span>at GE Healthcare<!---->
        </p>
      
</div>

我试过了:

currentDsgn = []
currentDesignations = browser.find_elements_by_class_name('linked-area flex-1')
    print(currentDesignations)

    for currentDesignation in currentDesignations:
        print(currentDesignation)
        currentDsgn.append(currentDesignation.text.strip())

但我得到了一个空列表。

【问题讨论】:

    标签: python selenium automation selenium-chromedriver


    【解决方案1】:

    试试这个 xpath:

    //div[contains(@class, 'linked-area')]/p[contains(@class, 'entity-result__summary')]
    

    并像下面这样使用它:

    currentDesignations  = browser.find_elements_by_xpath("//div[contains(@class, 'linked-area')]/p[contains(@class, 'entity-result__summary')]")
    for currentDesignation in currentDesignations:
       print(currentDesignation.get_attribute('innerHTML'))
    

    【讨论】:

    • 谢谢,这成功了!如果我们直接转到 p 类 'entity-result__summary' ,而不是从 div 类 'linked'area' 开始呢?
    • 是的@ShridharK 我们可以做到。只需确保在元素结构中(按 F12 时)以及使用上述 xpath 时,应突出显示所需的元素
    • 我们可以在不使用'contains'关键字的情况下编写代码吗?类似:@class = 'linked-area'@class = 'entity-result-summary'?
    • 不,我们不能。如果我们写@class = 'linked-area Selenium 将寻找完全匹配,我们想传递类的子部分因此我们使用contains
    • 这是有道理的
    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 2011-05-08
    • 2013-08-14
    • 2014-01-15
    • 2015-09-14
    • 2016-03-13
    相关资源
    最近更新 更多