javascript - 如何使用 xpath 获得硒中的非罢工价格金额?
问题描述
这是我试图自动化的网页的网址:
http://www.qaclickacademy.com/courses-description.php
我想使用 Selenium 和 XPath 定位器获取非删除线价格(目前为 20.00 美元)的价值。
包含我感兴趣的元素的 HTML 标记片段是:
<div class="course row" data-scroll-reveal=""
style="-webkit-transform: translatey(24px);transform: translatey(24px);opacity: 0;-webkit-transition: -webkit-transform 0.66s ease-in-out 0s, opacity 0.66s ease-in-out 0s;transition: transform 0.66s ease-in-out 0s, opacity 0.66s ease-in-out 0s;-webkit-perspective: 1000;-webkit-backface-visibility: hidden;"
data-scroll-reveal-initialized="true">
<div class="col-sm-4">
<a href="course-detail.php?id=130&t=websecurity-testing-for-beginners-qa-knowledge-to-next-level">
<img src="/courses-description.php?show=130" alt="websecurity-testing-for-beginners-qa-knowledge-to-next-level" class="img-responsive" width="186" height="123">
</a>
</div>
<div class="col-sm-8">
<div class="row">
<div class="col-md-9 col-sm-8">
<h3>
<a href="course-detail.php?id=130&t=websecurity-testing-for-beginners-qa-knowledge-to-next-level">
WebSecurity Testing for Beginners-QA knowledge to next level
</a>
</h3>
<div class="meta">
<span><i class="fa fa-user"></i><a href="#">Rahul Shetty</a></span>
<span><i class="fa fa-file-text"></i>60 Lessons</span>
<span><i class="fa fa-folder"></i><a href="#">Penetration testing</a></span>
</div>
</div>
<div class="col-md-3 col-sm-4 price">
<del style="font-size:15px;color:#aaa">$ 85.00</del>
<br>
$ 20.00
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p class="course-desc">
Course Launch Date : Aug 30th 2015 -Its Time to Protect our Websites from Security Attacks This Tutorial will give all the weapons you needed to investigate and
unlock the Security Holes in the Web applicationCourse lectures are conceptually driven with root level explanations and bring you to the level where you can
bring out the security bugsCourse Contents: Basics of Security Testing...
<br>
<a href="course-detail.php?id=130&t=websecurity-testing-for-beginners-qa-knowledge-to-next-level">
Read More
<i class="fa fa-angle-right"></i>
</a>
</p>
</div>
</div>
</div>
<div class="col-md-12">
<hr>
</div>
</div>
我尝试了很多方法,但到目前为止我一直无法找到解决方案
解决方案
这是python中的方法,它只会得到价格(20.00美元)。注意:这不适用于您是否有定价的两种情况。
def get_text_exclude_children(element):
return driver.execute_script(
"""
var parent = arguments[0];
var child = parent.firstChild;
var textValue = "";
while(child) {
if (child.nodeType === Node.TEXT_NODE)
textValue += child.textContent;
child = child.nextSibling;
}
return textValue;""",
element).strip()
如何使用这里。
element = driver.find_element_by_xpath("(//div[@class='col-md-3 col-sm-4 price'])[1]")
price = get_text_exclude_children(element)
推荐阅读
- django - 一些数据(1k-3k)的 Django 查询耗时太长(分钟)
- ios - Flutter:为什么卡片小部件之间的间距不同
- android - 为什么从 android 的谷歌驱动器下载 PDF 文件需要这么长时间?
- c# - 如何在 Entity Framework Core 3.0 中播种?
- servicestack - 是否有类似 HTTPContextEnricher 的东西适用于 Servicestack 和 Serilog
- javascript - 如何导入在另一个服务器上托管的另一个 html 页面中的 html 文件中定义的 Web 组件(自定义 html 元素)
- c# - 我如何更改使用 x = += -(int)Settings.ABtn; 递减数字的代码?所以 x 永远不会小于零?
- python - pytest:是否可以在 Class setup/teardown 中使用 pytest-datadir 固定装置
- java - CloudFoundry 中部署的线程池代码不起作用
- java - 使用 com.fasterxml.jackson.core.JsonParser 作为 Jersey 客户端序列化器/反序列化器?