0

I'm trying to get the dates on ebay listings (see green square around date in picture) using selenium and find_element_by_xpath but I am having a hard time. I'm trying to get the dates for each listing. I am using Python and Firefox browser.

I thought I could just do the following:

date_x = (driver.find_element_by_xpath("(//span[@class='s-item__ended-date s-item__endedDate'])[2]").text)

but it doesn't work. Not sure how to grab each date, is there something I am doing wrong or an easier way of doing this; if someone could help advise, would be appreciated.

enter image description here

YuanL
  • 67
  • 8

1 Answers1

1

Your question was a bit unclear to me, wasn't sure if you wanted a specific date or every date on the page. I will give you a solution for every date. I find that searching by class name is much simpler, and I find it more convenient.

for element in driver.find_elements_by_class_name('s-item__time-end'):
    print(element.text)

I entered eBay and it was a bit different for me, namely, the class of the end date was different so I used that instead, but you can change to the class that is necessitated by your needs. Also, instead of printing you probably want to store it in an array.

dates = []
for element in driver.find_elements_by_class_name('s-item__time-end'):
    dates.append(element.text)

As for the code you submitted, there's a syntax error. It should be:

date_x = driver.find_element_by_xpath("(//span[@class='s-item__ended-date s-item__endedDate'])[2]"), at least as far as I'm concerned. 
Skygear
  • 90
  • 6
  • I tried what you did and for some reason and it's still not working. Not sure what the issue might be, I am using Firefox on Mac OS, not sure if that makes a difference. – YuanL Dec 03 '20 at 05:28
  • Is there a rule on when you can use the '[1]' after? I've used it in a div[..][1] in the past and it has worked so i'm confused why it's no longer working – YuanL Dec 03 '20 at 05:29
  • Did you adjust the classes you're looking for? Also, tbh, I'm pretty new myself and I'm inexperienced with xpaths, that's why I gave you a find_by_class_name() solution, because it's much simpler, at least for the purpose of extracting the same elements. Perhaps if you give me a link to the exact page you were looking at I could try to help furthermore, because I didn't necessarily get quite the same format/html. – Skygear Dec 03 '20 at 05:38
  • 1
    I somehow got it to work, I think there was a syntax error somewhere because I wrote it out again slowly and it works again. Thanks for your help though, seriously appreciate it. ```date_x = (driver.find_element_by_xpath("(//span[@class='s-item__ended-date s-item__endedDate'])["+ str(item+1) +"]").text)``` I used item as a variable but basically [1],[2], etc – YuanL Dec 03 '20 at 05:44