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')
5
  • Instead of all the prose, post a minimal reproducible example including the HTML. Commented Mar 17, 2019 at 4:25
  • 1
    Possible duplicate of How do I iterate through elements in Selenium and Python? Commented Mar 17, 2019 at 4: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. Commented Mar 17, 2019 at 5: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. Commented Mar 17, 2019 at 5:28
  • Okay. I added both of the expressions that I tried. Neither of which returned anything when I tried to run them. Commented Mar 17, 2019 at 5:34

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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.
I thought you wanted all of the image sources on the page?
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.
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.
what do you mean by "All of the ones under the flow history"?
|
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
    

2 Comments

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.