0

I am attempting to get the title attribute for every listing on a market.

Element:

<a href="/goods/857529?from=market#tab=selling" title="MP9 | Food Chain (Minimal Wear)" xpath="1">

My code:

xpath = "//body/div[@class='marketlist']/div[@class='l_Layout']/div[@id='j_market_card']/div[@id='j_list_card']/ul[@class='card_csgo']/li[1]/a[1]"
for itemName in driver.find_elements("xpath", xpath):
    itemName = itemName.get_attribute("title")
    print(itemName)

Right now only the first element is being located on the page, although all of the other listings share the exact same xpath. Is there any reason the other listings aren't being located?

Also, is there a better way to locate all of the elements than by xpath? I know link text can be used but each title is different, so I wouldn't be able to search by link text.

ShadowCrafter_01
  • 533
  • 4
  • 19
  • _although all of the other listings share the exact same xpath_ That's not possible. Each element has its own unique xpath. I'm guessing the final `a` element in the xpath should be `a[1]` on the first loop, then `a[2]` on the second loop, etc. – John Gordon Apr 29 '23 at 22:06
  • I'm using SelectorsHub extension to copy Relative Xpath, which shows same xpath for each element of this type for each listing. I just tried changing the value at the end like you suggested and it resulted in no output. – Dallin Kump Apr 29 '23 at 22:12
  • I don't know what "SelectorsHub extension" is, so maybe I just don't understand what you're trying to do. But I do know that the xpath of each element absolutely should be unique. xpath is, more or less, a set of directions (like a map) of how to get to a specific element. There's no way the same set of directions could lead you to arrive at different elements. – John Gordon Apr 29 '23 at 22:29
  • Also, you said you're using the "Relative Xpath", however ... there's nothing relative in the actual xpath you used. So I'm doubly confused. – John Gordon Apr 29 '23 at 22:31
  • I think you answered my questions without meaning to. Now using absolute xpath, which is / html[1] / body[1] / div[5] / div[1] / div[4] / div[1] / ul[1] / li[x] / h3[1] / a[1], I can increment the li value and move between the listings. Thanks for your help! – Dallin Kump Apr 29 '23 at 22:54
  • @JohnGordon `But I do know that the xpath of each element absolutely should be unique.` This isn't really true. `There's no way the same set of directions could lead you to arrive at different elements.` This definitely isn't true. Easy example is `//div`. You CAN create an XPath that is unique (will only ever find a single element) but you can just as easily create an XPath that will return more than one element. Clearly OP is trying to create a single XPath that will return multiple elements. – JeffC Apr 29 '23 at 22:56
  • @DallinKump Using a absolute XPath is a bad idea. It's very fragile... even the smallest change to the HTML of the page can break it. Can you provide a link to the page? I can help write a relative XPath or CSS selector that will find what you are looking for and also make your code simpler. – JeffC Apr 29 '23 at 22:58
  • 1
    @JeffC I understand that e.g. `//div` can return more than one element. I suppose I expressed myself poorly. I was objecting to the statement "... all of the other listings share the exact same xpath", which should be impossible, given that the xpath in the question is absolute. – John Gordon Apr 29 '23 at 23:12
  • The XPath you are using can't be same for every element that you are trying to locate. You need to come up with an xpath which will select them all. Try this by going to dev console and searching elements by XPath, see if it will find everything that you are looking for. Paste the full HTML of what you are trying to parse to let others help you – levangode Apr 30 '23 at 00:50
  • @JeffC I would really appreciate that. You would need an account on the site to be able to access the page that I am on. Here is a link to a sceenshot of the complete HTML https://i.stack.imgur.com/l2ykz.png – Dallin Kump Apr 30 '23 at 15:33

1 Answers1

0

From the HTML provided, it looks like the CSS selector

#j_list_card li > a
^ # indicates an ID
            ^ a space indicates a descendant
                ^ a > indicates a child

would work.

See the W3C spec for a good references on CSS selector syntax.

JeffC
  • 22,180
  • 5
  • 32
  • 55