【发布时间】:2018-06-01 11:24:10
【问题描述】:
我正在尝试从以下 html 代码的最后一行中选择 I NEED THIS TEXT,但目前尚未成功:
<div class="warn">
<div class="row container pv2">
<div class="col xs12">
<div class="display-table-cell b-text_copy-4 pr1">
<i class="msg-icon b-icon b-icon-warn"></i>
</div>
<div class="display-table-cell b-text_copy-2 b-text_weight-bold">
<div> I NEED THIS TEXT <a href=https://somelink/contact.html target=_blank>contact us</a>.</div>
INPUT我使用以下方法失败了:
# all lines also tested without .text suffix
text1 = driver.find_element_by_class_name("display-table-cell.b-text_copy-2.
b-text_weight-bold").text
text2 = driver.find_element_by_class_name("warn").text
text3 = driver.find_element_by_class_name("col.xs12").text
text4 = driver.find_element_by_class_name("display-table-cell").text
text5 = driver.find_element_by_xpath("//*[contains(@text='I NEED THIS TEXT')]")
输出:
text1: Message: Unable to locate element: .display-table-cell.b-text_copy-2.b-text_weight-bold
text2: Message: Unable to locate element: .warn
text3: Message: Unable to locate element: .col.xs12
text4: Message: Unable to locate element: .display-table-cell
text5: Message: Given xpath expression "//*[contains(@text='I NEED THIS TEXT')]" is invalid: [Exception... "<no message>" nsresult: "0x8060000d (<unknown>)" location: "JS frame :: chrome://marionette/content/element.js :: element.findByXPath :: line 355" data: no]
我做错了什么吗?如何选择我需要此文本并将其打印到控制台?
【问题讨论】:
标签: python selenium html-content-extraction