【发布时间】:2016-05-23 01:59:43
【问题描述】:
我正在编写一个获取职位名称的 Python 脚本。列表类中的职位名称示例如下所示:
为了获取诸如 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&mode=profile&id=c2c0dd779fe969740566778ee0816177&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&id=3b11ddb0571c6c4908b2cca98804ec00&s=jobs&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&ss=list&mode=profile&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&id=3b11ddb0571c6c4908b2cca98804ec00&s=jobs&ss=jobmatches" class="ListPrimaryLink">Summer 2016 Co-op - Software Test Engineer Intern</a>**
如何获得最高父类(列表),甚至获得其他 div 类?
【问题讨论】:
标签: python selenium xpath selenium-webdriver selenium-chromedriver