python - Python 中按 xpath 查找元素

标签 python xpath webdriver

我正在搜索网页的一部分:

<TR class='title'><TD colspan=3 bgcolor=#C0C0C0>Order number 6097279</TD></TR>

我想提取数字 - 该数字每次都会从文本中更改(即:“订单号 6097279”为我提供字符串“6097279”)

我已尝试以下操作并收到“无法定位元素”错误:

order_number = order_products.find_element_by_xpath("//TR[@class='title']");

回溯如下:

in call_orderpage(https://www.daz3d.com/i/account/orderdetail?order=8104987)
Failed!
Error (NoSuchElementException): Message u'Unable to locate element: {"method":"xpath","selector":"//tr[@class=\'title\']"}'
Program finished!

TR 更改为 tr 不会产生任何影响。

为什么这不起作用?我还有其他使用“class = 短语”的 find_element_by_xpath 搜索,并且它们有效。


我正在使用的代码:

order_number = order_products.find_element_by_xpath("//TR[@class='title']");

根据我所读到的内容,xpath元素名称不区分大小写。但无论哪种情况我都会遇到同样的错误。

我得到的回复:

enter image description here

至于不“接受”答案,我对此表示歉意 - 部分是因为我不知道我必须这样做,另一部分是当有回复时我没有收到通知。

我会回去修正。

========================================

编辑以回应 gfortune...

I updated your question with the information you provided in an answer. In the future, either reply here in a comment or edit your question directly and update it to contain the additional information. Editing your question is the best approach. I've submitted an edit for peer review so hopefully that shows up soon. Unfortunately, we're still missing a fair amount of the context for your question. A) What library are you using? B) More code. One line probably isn't enough. C) More information on the page you are parsing. Ideally, a very short test case that triggers the error. – gfortune

老实说,“评论”面板中的小字很难阅读。另外,我没有收到已做出答复的通知...

无论如何,我没有使用 lxml - 因为我不明白它是什么。现在我有了更好的想法,我将研究它是如何工作的,谢谢。

“order_products 变量”只是整个 html 的子 block - 它是保存我需要使用的 hrtml 的部分。该页面本身有很多链接子列表和我不需要使用的东西。我将其分开,是为了在搜索我确实需要的数据时不用担心。

我认为我已经发布了对我的代码的更正,该代码确实有效 - 又是这样。

order_number = order_products.find_element_by_xpath("//tr[@class='title even']");

你会注意到“title Even”代替了“title”类...firepath 向我展示了那个隐藏的部分,它让我和 xpath 搜索都感到困惑...

我的代码变成了:

    order_number = order_products.find_element_by_xpath("//tr[@class='title even']");
order_number = order_number.text
order_number = order_number.replace('Order number ', '')
print '\nOrder number [' + order_number + ']'

将数字与“订单号”分开...

最佳答案

根据新信息,我将进行一些盲目猜测并开始得到答案,随着我们了解更多信息,我们可以对其进行改进。

首先,您似乎没有使用 lxml。我已经编写了一个在 lxml 中工作的解决方案,因此如果您能够切换到 lxml 来满足您的解析/xpath 需求,您应该能够直接使用它。如果没有,您可以提供一些有关为什么不使用 lxml 的信息。

其次,错误消息暗示该元素不存在。您确定您正在阅读的文档中存在带有 class='title' 的 tr 吗?针对您确定包含所需 html 的测试文件运行代码。我将提供一些有效的示例 html。

正如所 promise 的,下面是一个使用 lxml.html 解析示例文件并提取订单号的示例。如果有具体原因这不起作用,请在评论中发布相关信息,我会为您调整示例。如果您根本无法切换到 lxml,我们将需要评论中要求的更多信息。请根据需要编辑您的原始问题(下面有一个小编辑按钮)。

测试.py
import lxml.html

data = lxml.html.parse('test.html')

orders = data.xpath('//tr[@class="title"]/td')

for order in orders:
    print('Order text: ' + order.text)
    print('Parsed order number: ' + order.text.split(' ')[-1])

测试.html

<html>
<head><title>Test</title></head>
<body>
Blah blah
<div>Ignore me</div>
<div>Outer stuff
    <table border="1">
        <tr><td>bogus stuff we don't care about</td></tr>
        <tr class='title'><td color='grey'>Order Number 6097279</td></tr>
        <tr class='something_else'><td>Boring stuff</td></tr>
    </table>
</div>
</body>
</html>

输出

Order text: Order Number 6097279
Parsed order number: 6097279

关于python - Python 中按 xpath 查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9246769/

相关文章:

python - 比较和更新 pandas dataframe 列列表与来自另一列的字符串

python - 如何在 python 中编写嵌套并发代码?

python - 计算 python 中某个值相对于另一个值聚合的重复出现次数

xml - 如何复制 xml 元素

selenium - Webdriver 在 Gmail 中打开邮件

python - TensorFlow GPU Linux 安装

python - Xpath - 无法使用 Text() 获取信息,但我看不到路径和数字

python - 从滚动时添加新表格的页面中抓取 HTML 数据

c# - 如何在 Selenium 中使用 Telerik 下拉菜单?

java - Selenium Webdriver 警告 - 无效 token "screen"