简体   繁体   English

how do I select xpath image without a class name using selenium in python?

[英]how do I select xpath image without a class name using selenium in python?

How do i select an image xpath without a classname.我如何 select 图像 xpath 没有类名。 HTML code is like this HTML代码是这样的

<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如果我右键单击并复制 xpath 它会给我这个//*[@id="sortable-results"]/ul/li[1]/a/img但是当我在我的代码中使用它时它有一些错误

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)但是当我print(src)时它返回一个[] (src)

Full div完整的 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>

The xpath is close. xpath 很接近。 You need to use // at the beginning of the path and remove the /@src您需要在路径的开头使用//并删除/@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如果要确保元素具有src属性,就像这样

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

To get the src attribute use get_attribute('src)要获取src属性,请使用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.注意find_elements返回列表,使用 index 获取第一个元素。

If you want to use class="result-info" to locate the element you can do如果你想使用class="result-info"来定位你可以做的元素

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')

Actually the xpath has been copied correctly, You have used it in a wrong way in the fetch code.实际上 xpath 已正确复制,您在获取代码中以错误的方式使用它。

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:或者,如果您想要相同 xpath 类型的所有图像的列表,请使用:

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

(ie 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) (即删除特定数量的 'li' div 或您想要概括并使用find_elements的任何其他 div;您需要使用find_element来获取特定的单个元素)


To get the attribute 'src', use get_attribute method:要获取属性 'src',请使用get_attribute方法:

For case 1:对于案例 1:

website = image.get_attribute('src')

For case 2:对于案例 2:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用 Xpath Selenium Python 的情况下单击 - How to click without using Xpath Selenium Python EASY PYTHON SELENIUM:如何点击 class 名称? - EASY PYTHON SELENIUM: How do I click by class name? 如何使用 python 中的 selenium 从 class 名称中获取值? - How do I get a value from a class name using selenium in python? 如何在 python 的 selenium 中使用 xpath 选择具有特定文本的跨度的第二个实例? - How do you select the 2nd instance of a span with a specific text using xpath in python's selenium? 如何在不使用 XPath 的情况下使用 selenium 获取元素 Datetime? - How do I get the element Datetime using selenium without using XPath? 如何选择具有此功能的 div - how do I select the div that has this <i> using Selenium python Python,Selenium 尝试通过类 xpath 定位图像 - Python, Selenium trying to target image by class xpath 如何使用带有 Selenium 和 Python 的 find_elemets_by_xpath() select 节点 - How to select nodes using find_elemets_by_xpath() with Selenium and Python 如何将 select 元素在 Selenium 上使用 XPATH 语法用于 Python? - How to select element using XPATH syntax on Selenium for Python? 如何使用 python 从 selenium 的下拉列表中 select 项目? - How do I select an item from dropdown in selenium using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM