按多个类的HREF属性查找页面上的元素

2024-06-16 13:22:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Selenium库和python。 在页面上,我得到了多个报价,每个报价都有相同的链接开头https://www.example.com/offer/。。。。 和相同的类,但问题是我找不到包含多个类的元素。它还具有相同的属性“data cy”

我试过了

offerName = driver.find_element_by_xpath("//a[contains(@class, 'marginright5') and contains(@class, 'link') and contains(@class, 'linkWithHash') and contains('detailsLink')]")

但由于这个错误,它不起作用

selenium.common.exceptions.InvalidSelectorException: Message: Given xpath expression "//a[contains(@class, 'marginright5') and contains(@class, 'link') and contains(@class, 'linkWithHash') and contains('detailsLink')]" is invalid: [Exception... "<no message>"  nsresult: "0x8060000d (<unknown>)"  location: "JS frame :: chrome://marionette/content/element.js :: element.findByXPath :: line 387"  data: no]

我怎样才能找到他们


Tags: andnodata链接seleniumlink页面element
1条回答
网友
1楼 · 发布于 2024-06-16 13:22:15

就像错误消息说xpath无效一样, 我可以在最后一个“contains”中看到一个错误

尝试更改此选项:

driver.find_element_by_xpath("//a[contains(@class, 'marginright5') and contains(@class, 'link') and contains(@class, 'linkWithHash') and contains('detailsLink')]"

为此:

driver.find_element_by_xpath("//a[contains(@class, 'marginright5') and contains(@class, 'link') and contains(@class, 'linkWithHash') and contains(@class, 'detailsLink')]"

相关问题 更多 >