如何通过属性值使用XPath获取元素

huangapple go评论103阅读模式
英文:

how to get element with xpath by attribute value

问题

此页面尝试获取下一页箭头链接。然而,response.selector.xpath('//a[aria-label="Next page"]')返回的是[]。帮助! 如何通过属性值使用XPath获取元素
我能够用response.selector.xpath('//*[contains(@class, "Pagination_arrowItem")]/a')[1].attrib['href']来解决,但是如果是第一页并且只显示一个箭头,则会遇到问题。

英文:

trying to get the next page arrow link from this page. however
response.selector.xpath('//a[aria-label="Next page"]') yields []. help! 如何通过属性值使用XPath获取元素
I was able to solve it with response.selector.xpath('//*[contains(@class, "Pagination_arrowItem")]/a')[1].attrib['href'] but then run into an issue if it's the firts page and only one arrow is shown.

答案1

得分: 1

你应该能够通过以下方式获取它:

response.selector.xpath('//a[@data-e2e-test="pagination-next"]')

更多信息,请查看Scrapy文档

英文:

You should be able to get it with:

response.selector.xpath('//a[@data-e2e-test="pagination-next"]')

See Scrapy documentation for more.

答案2

得分: 1

Since the anker itself is using a workable @data-e2e-test attribute, its name seems a bit temporary. Therefore you also could use:

// *[starts-with(@class, "Pagination_container")]/div[position()=last()]/a

英文:

Since the anker itself is using a workable @data-e2e-test attribute, its name seems a bit temporary. Therefore you also could use:

//*[starts-with(@class,"Pagination_container")]/div[position()=last()]/a

huangapple
  • 本文由 发表于 2023年8月10日 15:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定