获取元素Python Selenium的祖父类

2024-05-14 15:52:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写一个Python脚本来获取职位头衔。列表类中的职务示例如下所示:

Job Title ex.

为了获取诸如Summer之类的关键字,或者在本例中,我分别使用了xpath,如下所示:

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

我想抓取文本,显示它在哪里,而不是试图抓住具体的,因为每个工作将在我的程序变化。基本上,我想获取grandian类并选择包含作业位置的div类(或者只获取同一父类中的其他元素),但是,使用xpath和“..”和

^{2}$

只返回当前类而不是整个list 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类呢?在


Tags: httpsdividitemxpathlistclassamp
1条回答
网友
1楼 · 发布于 2024-05-14 15:52:25

与xpath只选择a标记不同,您可以使用一个包含元素的祖先的xpath(例如li)。在

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

相关问题 更多 >

    热门问题