Grzegorz Barański
Grzegorz Barański

Reputation: 25

Find elements on page by HREF attribute of by multiple classes

i am using Selenium library and python. On page i got multiple offers, every has same beginning of the link https://www.example.com/offer/.... And same classes, but the problem is i couldn't find element which has more than one class. It also has the same attribute "data-cy"

I tried

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

But it didn't worked because of this error

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]

How i can find them?

Upvotes: 2

Views: 152

Answers (1)

JaviPako
JaviPako

Reputation: 56

Well Like the error message says your xpath is invalid, I can see one error in the last 'contains'.

Try change this:

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

To this:

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

Upvotes: 4

Related Questions