【问题标题】:Getting grandfather class of an element - Python Selenium获取元素的祖父类 - Python Selenium
【发布时间】:2016-05-23 01:59:43
【问题描述】:

我正在编写一个获取职位名称的 Python 脚本。列表类中的职位名称示例如下所示:

Job Title ex.

为了获取诸如 Summer 之类的任何关键字,或者在本例中我单独获取了这个标题,我使用了如下所示的 xpath:

    temp = web.find_elements_by_xpath("//*[contains(text(), 'Summer 2016 Co-op - Software Test Engineer Intern')]")

我想抓取显示它所在位置的文本,而不是专门抓取它,因为每个作业都会在我的程序中发生变化。基本上我想抓取祖父类并选择包含作业位置的 div 类(或者只是抓取同一父类中的其他元素),但是,使用 xpath 和 '..' 和

web.get_attributes('outerHTML')

只返回当前类而不是整个列表 div 类。

这个 div 类的代码如下所示:

<li class="row list-item list_rows" id="row_3b11ddb0571c6c4908b2cca98804ec00">

        <div class="list-item-logo">
                                <a href="https://rit-csm.symplicity.com/students/index.php?s=employers&amp;mode=profile&amp;id=c2c0dd779fe969740566778ee0816177&amp;ss=jobmatches"><img src="./job postings _ RIT Job Zone_files/view.php" alt="Logo" class="avatar avatar-square"></a>
                        </div>

        <div class="list-item-body">

            <div class="list-item-title">
                **<a href="https://rit-csm.symplicity.com/students/index.php?mode=form&amp;id=3b11ddb0571c6c4908b2cca98804ec00&amp;s=jobs&amp;ss=jobmatches" class="ListPrimaryLink">Summer 2016 Co-op - Software Test Engineer Intern</a>**                                 

            </div>

            <div class="list-data-columns">                                                         
                Co-op
            </div>
            <div class="list-data-columns">
                <a href="https://rit-csm.symplicity.com/students/index.php?s=employers&amp;ss=list&amp;mode=profile&amp;id=c2c0dd779fe969740566778ee0816177" class="ListPrimaryLink">Veson Nautical</a> - Boston, Massachusetts<br>Lebanon, New Hampshire               
                 </div>                                                                                                                                                                             
              </div>     
            </div>
        </div>
    </li>

显然没有加粗的行是使用 xpath 代码行选择的行。

**<a href="https://rit-csm.symplicity.com/students/index.php?mode=form&amp;id=3b11ddb0571c6c4908b2cca98804ec00&amp;s=jobs&amp;ss=jobmatches" class="ListPrimaryLink">Summer 2016 Co-op - Software Test Engineer Intern</a>** 

如何获得最高父类(列表),甚至获得其他 div 类?

【问题讨论】:

    标签: python selenium xpath selenium-webdriver selenium-chromedriver


    【解决方案1】:

    除了选择只选择a 标记的xpath 之外,您还可以使用带有元素祖先的xpath(例如li)。

    //*[contains(text(), 'Summer 2016 Co-op - Software Test Engineer Intern')]/ancestor::li[contains(@id, 'row_')]
    

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2021-12-29
      相关资源
      最近更新 更多