使用 Selenium 从 webelements 列表中获取 src 值
您好,尝试改编此视频中的解决方案
#scroller 100 fois pour reveler le plus d'image ( comment etre sur qu'on est à la fin ?)
n_scrolls = 100
for i in range(1, n_scrolls):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5)
#recupère toutes les balises img puis leurs attribut src (je ne comprend pa bien cette façon d'assigner les elements)
images = driver.find_elements_by_tag_name('img')
print(images)
images = [images.get_attribute('src') for img in images]
但输出是:
[<selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="4a4b2838-67d6-4787-a168-9e25e948a21a")>, <selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="7e3ae8f5-160a-4da6-b3a7-2be10bad8f3f")>, <selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="8c45c421-3f25-4498-85a2-565506835984")>, <selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="5ef69be7-13d7-41f1-9ec8-d8ac6e843fdd")>, <selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="626ae474-bccc-40c3-ac60-5b5f021b7bf0")>, <selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="ea0b589d-8e90-470a-ad59-a661f8d52b31")>
是否可以获取 src 属性,而不是 img 标签元素?
Hello trying to adapt a solution from this video
#scroller 100 fois pour reveler le plus d'image ( comment etre sur qu'on est à la fin ?)
n_scrolls = 100
for i in range(1, n_scrolls):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5)
#recupère toutes les balises img puis leurs attribut src (je ne comprend pa bien cette façon d'assigner les elements)
images = driver.find_elements_by_tag_name('img')
print(images)
images = [images.get_attribute('src') for img in images]
But the output is:
[<selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="4a4b2838-67d6-4787-a168-9e25e948a21a")>, <selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="7e3ae8f5-160a-4da6-b3a7-2be10bad8f3f")>, <selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="8c45c421-3f25-4498-85a2-565506835984")>, <selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="5ef69be7-13d7-41f1-9ec8-d8ac6e843fdd")>, <selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="626ae474-bccc-40c3-ac60-5b5f021b7bf0")>, <selenium.webdriver.remote.webelement.WebElement (session="50f35cc8388f3b59df4042bcc09c7079", element="ea0b589d-8e90-470a-ad59-a661f8d52b31")>
Instead of the img tag element, is it possible to get the src attributes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以找到你所犯的错误。
而不是这个
它应该是
因为你正在迭代列表。
现在打印列表,您将获得所有 src 值。
打印(图像)
I can find the mistake you have made.
Instead of this
It should be
Since you are iterating the list.
Now print the list you will get all
src
values.print(images)
由于
images
是![]()
元素的列表,同时迭代 WebElements 在for
循环中,您需要从每个元素中提取src
属性img,也可能希望避免编辑相同的列表和完全创建一个不同的列表。实际上,您的代码行将是:As
images
is the list of the<img>
elements, while iterating the WebElements within thefor
loop you need to extract thesrc
attribute from each individual img and also may like to avoid editing the same list and create a different list altogether. So effectively, your line of code will be: