1

How do i select an image xpath without a classname. HTML code is like this

<img alt="" class src="https://images.craigslist.org/00J0J_i9BI6mN6rKP_300x300.jpg">

If I right click and copy xpath it gives me this //*[@id="sortable-results"]/ul/li[1]/a/img but when I use it in my code it has some error

In my code i use like this

 src = driver.find_elements_by_xpath('/li[@class="result-row"]/a[@class="result-image gallery"]/img[@class=""]/@src')

but it returns me an [] when i print(src)

Full div

<li class="result-row" data-pid="7017735595">

        <a href="https://vancouver.craigslist.org/van/ele/d/vancouver-sealed-brand-new-in-box/7017735595.html" class="result-image gallery" data-ids="1:00J0J_i9BI6mN6rKP"><img alt="" class="" src="https://images.craigslist.org/00J0J_i9BI6mN6rKP_300x300.jpg">
                <span class="result-price">$35</span>
        </a>

    <p class="result-info">
        <span class="icon icon-star" role="button" title="save this post in your favorites list">
            <span class="screen-reader-text">favorite this post</span>
        </span>

            <time class="result-date" datetime="2019-11-11 00:52" title="Mon 11 Nov 12:52:25 AM">Nov 11</time>


        <a href="https://vancouver.craigslist.org/van/ele/d/vancouver-sealed-brand-new-in-box/7017735595.html" data-id="7017735595" class="result-title hdrlnk">Sealed - Brand New in Box - Google Home Mini</a>


        <span class="result-meta">
                <span class="result-price">$35</span>


                <span class="result-hood"> (Vancouver)</span>

                <span class="result-tags">
                    <span class="pictag">pic</span>
                </span>

                <span class="banish icon icon-trash" role="button">
                    <span class="screen-reader-text">hide this posting</span>
                </span>

            <span class="unbanish icon icon-trash red" role="button" aria-hidden="true"></span>
            <a href="#" class="restore-link">
                <span class="restore-narrow-text">restore</span>
                <span class="restore-wide-text">restore this posting</span>
            </a>

        </span>
    </p>
</li>
3
  • can you share the whole div where your image is located? you can create an absolute path using xpath. Commented Nov 11, 2019 at 9:20
  • ok sir ill post the whole html for that div Commented Nov 11, 2019 at 9:21
  • 1
    when I use it in my code it has some error What is it? and how do you use it? Commented Nov 11, 2019 at 9:21

2 Answers 2

3

The xpath is close. You need to use // at the beginning of the path and remove the /@src

//li[@class="result-row"]/a[@class="result-image gallery"]/img[@class=""]

If you want to make sure the element has src attribute it's like that

//li[@class="result-row"]/a[@class="result-image gallery"]/img[@class=""][@src]

To get the src attribute use get_attribute('src)

src = driver.find_elements_by_xpath('//li[@class="result-row"]/a[@class="result-image gallery"]/img[@class=""]')[0].get_attribute('src')

Note that find_elements return list, use index to get the first element.

If you want to use class="result-info" to locate the element you can do

elements = driver.find_elements_by_xpath('//p[@class="result-info"]/../a[@class="result-image gallery"]/img[@class=""]')
for element in elements:
    src = element.get_attribute('src')
Sign up to request clarification or add additional context in comments.

11 Comments

how do I get the image src the one that ends with jpg file in order for me to save the image or the url in it? I tried to print(src) it returns me ` <selenium.webdriver.remote.webelement.WebElement (session="a1858a5fdf3e236394be883fc8f6bc5a", element="0ab23f0f-5420-498c-bcfd-22af33872247")>`
i want to get the image attribute src
oh i see, so i can add another function like get_attribute i didint know that. Thanks for the answer sir. Can you explain it to me deeper? why you used index 0 array and other things
@Vince get_attribute is part of Selenium, you don't need to add anything. The index 0 is already explained in the last line.
ya sir. i already figured it out. Thanks a lot for your answers !!
|
2

Actually the xpath has been copied correctly, You have used it in a wrong way in the fetch code.

If you want the specific image, use

image = driver.find_element_by_xpath('//*[@id="sortable-results"]/ul/li[1]/a/img')

Or, if you want a list of all images of same xpath type, use:

images = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/a/img')

(i.e. remove the specific number of 'li' div or any other div that you want to generalise and use find_elements; you need to use find_element for fetching a specific single element)


To get the attribute 'src', use get_attribute method:

For case 1:

website = image.get_attribute('src')

For case 2:

website = images[0].get_attribute('src')

10 Comments

but i want the image src how could i achieve that?
got me an error 'list' object has no attribute 'get_attribute'
Updated the answer. If you fetch using find_elements, it fetches a list. So you need to select the element from list, i.e. list[0] in this case. If you fetched using find_element though, you would need to select from a list and you can directly get attribute
i dont get the output i want sir. It just gives me one image url but in a different form. like every letter there is a new line. its like this h t t p s : / / i m a g e s . c r a i g s l i s t . o r g / 0 0 R 0 R _ x w 4 R v C R Y D B I just want to list all the image url in every result-info
I am sure you are printing it wrong. If you want 'src' for all the images fetched in the list, use a loop. Like 'for image in images: website = image.get_attribute('src'); print(website)'
|

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.