[英]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.