【问题标题】:How can I express the following line of HTML code in Xpath?如何在 Xpath 中表达以下 HTML 代码行?
【发布时间】:2021-12-16 06:37:10
【问题描述】:

代码行如下:

<input type="hidden" id="data_mentalidad_web_35084" value='[{"name":"Extensor Cable Riser PEC 300 PCI-E x16 ","id":"35084","price":"29990","brand":"Deepcool  \u00ae ","category":"Tarjetas Gr\u00e1ficas","variant":"","quantity":1,"position":1}]'>

我只想要以粗体标记的内容:

<input type="hidden" id="data_mentalidad_web_35084" value='[{"name":"***Extensor Cable Riser PEC 300 PCI-E x16*** ","id":"35084","price":"***29990***","brand":"Deepcool  \u00ae ","category":"***Tarjetas Gr\u00e1ficas***","variant":"","quantity":***1***,"position":1}]'>

【问题讨论】:

  • "quantity":***1*** 是无效的语法。要在你需要一根绳子的人周围装星星:f"***{1}***"

标签: python selenium web-scraping xpath


【解决方案1】:

您可以多次使用containsand,如下所示:

//input[contains(@value,'Extensor Cable Riser PEC 300 PCI-E x16') and contains(@value,'29990') and contains(@value,'Tarjetas Gr\u00e1ficas') and contains(@value,'1')]

您还需要了解该值是 attribute,它只有一个值,包裹在 [] 中,不要对此感到困惑。

【讨论】:

  • type="hidden" 所以 Selenium 将无法立即找到它。交叉检查你的答案一次。
  • @DebanjanB :问题只是要求 XPath,Selenium 不是唯一使用 XPath 的工具。
  • 输出仍然是 [{"name":"Extensor Cable Riser PEC 300 PCI-E x16 ","id":"35084","price":"29990","brand":"Deepcool \u00ae ","category":"Tarjetas Gr\u00e1ficas","variant":"","quantity":1,"position":1}] 而不是 1 正如 OP 所期望的那样。
  • @DebanjanB:有一种发明叫JSON数组,以及如何将字符串转换为JSON数组并检索数据。
  • 这正是 OP 的主要问题。
猜你喜欢
  • 2014-06-19
  • 2017-04-27
  • 1970-01-01
  • 2015-02-06
  • 2020-02-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多