0

So I'm currently trying to find all the src links among a list of generated divs. The problem is it shares the same class name and alt tag as other elements on the page so I'm stuck using the xpath. But when I try to use it I am limited to only returning the value that is indexed in the xpath. For example div[3]. How could I get it to find all the elements div[1-inf] and not just one specific one? I discovered position() as a parameter but I haven't had much luck getting it to work. Maybe I'm just not using it right. driver.find_elements_by_xpath("//*[@id='tab-history-flow']/div[3]/a/img").get_attribute('src')

<div style="display:inline-block">
    <a target="_blank" title="Inventory Profile" href="http://csgo.exchange/profiles/76561197969720703">
    <img class="Avatar" alt="avatar" title="ArieBier | 2015-09-16 18:20:58" style="width:32px;height:32px" src="https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/df/dfd267c19d759f730e1051ae4657d0100a6b6c0d.jpg">
    </a>                                    
</div>
<div style="display:inline-block">
<a target="_blank" title="Inventory Profile" href="http://csgo.exchange/profiles/76561198136313290">
<img class="Avatar" alt="avatar" title="by | 2015-09-17 02:53:25" style="width:32px;height:32px" src="https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/5d/5d4c06db9ba89f8a945108af10075ebd348cd1ae.jpg">
</a>                                    
</div>
<div style="display:inline-block">
    <a target="_blank" title="Inventory Profile" href="http://csgo.exchange/profiles/76561198152970370">
    <img class="Avatar" alt="avatar" title="Marn | 2015-10-05 14:40:37" style="width:32px;height:32px" src="https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/ae/ae7878915405c9ad622c9d7fc3b52f3b71ed140a.jpg">
    </a>                                    
</div>

What I already tried.

driver.find_elements_by_xpath("//*[@id='tab-history-flow']/div/a/img"[position() < 1000]).get_attribute('src')
driver.find_elements_by_xpath("//*[@id='tab-history-flow']/div[position() < 1000]/a/img").get_attribute('src')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
CodeOrDie
  • 41
  • 1
  • 8
  • Instead of all the prose, post a [mcve] including the HTML. – kjhughes Mar 17 '19 at 04:25
  • 1
    Possible duplicate of [How do I iterate through elements in Selenium and Python?](https://stackoverflow.com/questions/45780533/how-do-i-iterate-through-elements-in-selenium-and-python) – Sers Mar 17 '19 at 04:41
  • Added the html. It's a pretty straight forward question. I'm just trying to figure out how I can select all of these divs at one time through the use of xpath. – CodeOrDie Mar 17 '19 at 05:24
  • I believe it would be easier for us to help you if you posted the code you've already tried, even if it didn't work. Debugging code that we can see is more efficient than trying to convert your explanation into code and debugging that. – Niayesh Isky Mar 17 '19 at 05:28
  • Okay. I added both of the expressions that I tried. Neither of which returned anything when I tried to run them. – CodeOrDie Mar 17 '19 at 05:34

2 Answers2

2

Well, I don't see any elements with id's much less the one you specified so I don't know exactly why it isn't working. However, I think this could be accomplished pretty easily. I noticed all of the img tags in the HTML you pasted in your question have class="Avatar". Here's how I would get the image sources:

images = driver.get_elements_by_css_selector('#tab-history-flow > div > a > img')
for image in images:
    image = image.get_attribute(src)

This should turn images into an array containing all your images sources. Let me know if this does/doesn't help.

Disclaimer: I haven't seen your whole HTML page, so the selector I used might not work. now I have, and I think that code should work for you.

C. Peck
  • 3,641
  • 3
  • 19
  • 36
  • Yeah sorry the id I specified is the container div that all those divs lie in. Well the issue I had was there are a few other images on the page as well with the same class and alt of Avatar. Which would also be returned but I'll give that a try and see if I can figure it out. – CodeOrDie Mar 17 '19 at 06:42
  • I thought you wanted all of the image sources on the page? – C. Peck Mar 17 '19 at 06:52
  • http://csgo.exchange/item/15534755273 Most of them. All of the ones under the flow history. But the two on the top left are also Avatars. – CodeOrDie Mar 17 '19 at 06:59
  • I'm thinking the best way would be to search for imgs with the class of Avatar with the style of width:32px;height:32px. Not 100% sure how that would look though. – CodeOrDie Mar 17 '19 at 07:05
  • what do you mean by "All of the ones under the flow history"? – C. Peck Mar 17 '19 at 07:22
  • All the small images under history at the bottom of the page. – CodeOrDie Mar 17 '19 at 07:26
  • `pics = driver.find_elements_by_css_selector("[style^=width]")` I'm having some luck with this. But it wont let me add 32 pixels to it. When I try it doesn't return anything. – CodeOrDie Mar 17 '19 at 07:28
  • Try the CSS selector I specified above, ('#tab-history-flow > div > a > img'). I think that gets all the images you want. – C. Peck Mar 17 '19 at 07:29
  • Hmm. Seems like it would work. But when I add the for statement and try to print the result I get nothing back. – CodeOrDie Mar 17 '19 at 07:44
  • If you want to print the result try adding ‘print(str(image))’ inside of the loop. – C. Peck Mar 17 '19 at 07:50
1

To print all the src attributes you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print([element.get_attribute('src') for element in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#tab-history-flow a[title='Inventory Profile']>img.Avatar[alt='avatar']")))])
    
  • Using XPATH:

    print([element.get_attribute('src') for element in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@id='tab-history-flow']//a[@title='Inventory Profile']/img[@class='Avatar' and @alt='avatar']")))])
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you. That works perfectly. I have one rather big problem though. What I'm trying to do is cycle through all these inventory pages and capture any items that have certain people in their history. Although I did want it to scan through all the srcs. What I really need it to do is to only return the src from the people that matched for that said item. But the way I have it set up now it returns every avatar from any of the items that had a profile matching in the history. And not just specifically the few people that I'm looking for. Kind of confusing. – CodeOrDie Mar 17 '19 at 22:33
  • @CodeOrDie Shouldn't be that tough per se but you have to raise a new question for your new requirement so contributors can help you out. – undetected Selenium Mar 17 '19 at 22:36