【问题标题】:Unable to get partial or wildcard equivalent xpath selector无法获得部分或通配符等效的 xpath 选择器
【发布时间】:2017-10-23 11:13:55
【问题描述】:

我正在尝试获取这 6 个选择器。我似乎只能得到8个。此外,它是动态的,因此会不断变化

//div[@class='sm-CouponLink']//div[@class='sm-CouponLink_Label'] 

页面不断变化到

//div[@class='sm-Coupon']//div[@class='sm-CouponLink'] 

以及它的其他变体。

最好是//div[@class*='sm-Coup'] 我试过使用通配符,但我相当肯定使用 xpath 不支持这个,我不想在这种情况下使用 CSS,因为我相当依赖它。 我也试过:

//div[starts-with(@class, ''sm-Coupo')]

我在这个页面上:

https://www.bet365.com.au/#/AS/B1/

【问题讨论】:

  • 抓取该网站以及许多带有数据的网站违反了他们的服务条款。

标签: python css python-3.x selenium selenium-webdriver


【解决方案1】:

试试 xpath:

//div[starts-with(@class, 'sm-CouponLink_Label')]
//div[contains(@class, 'sm-CouponLink_Label')]

//div[@class='sm-CouponLink']/div[starts-with(@class, 'sm-CouponLink_Label')]

如果您需要包装 div,也可以使用 XPath:

//div[@class='sm-CouponLink'][div[starts-with(@class, 'sm-CouponLink_Label')]]

或 CSS:

div.sm-CouponLink>div[class^='sm-CouponLink_Label']

PS:你的 xpath //div[starts-with(@class, ''sm-Coupo')] 几乎是正确的,除了它有额外的 ' 引用。这是有效的: //div[starts-with(@class, 'sm-Coupo')]

【讨论】:

  • 谢谢。 xpath 工作,然后我认为网页发生了变化(非常动态的页面)。 xpath 是否有 nth-child 等价物,例如 .sm-CouponLink_Label:nth-child(1)。这个页面非常动态,所以尽量保持 xpath 小
  • xpath 中的第 n 个元素:stackoverflow.com/questions/4007413/…
  • 我有类似的东西: //div[starts-with(@class, 'sm-Coupo')][1] 。不太理想,我可能需要找到一些关于更好地获取 xpaths 的教程
猜你喜欢
  • 2019-12-03
  • 2018-08-31
  • 2012-03-25
  • 2020-03-05
  • 2019-10-26
  • 1970-01-01
  • 2018-09-02
  • 2013-07-23
  • 2021-11-15
相关资源
最近更新 更多