Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否有更广泛的方法通过selenium在webelement中获取src?_Python_Selenium_Web Scraping - Fatal编程技术网

Python 是否有更广泛的方法通过selenium在webelement中获取src?

Python 是否有更广泛的方法通过selenium在webelement中获取src?,python,selenium,web-scraping,Python,Selenium,Web Scraping,我以前从其他Web元素中提取过“src”,但这一个似乎让我难以理解。我正试图抓住所有的链接到这个网页上的所有图像。然而,即使webelement由css选择器高亮显示,当我试图通过get_属性(“src”)获取它时,它也不会返回链接。如果我使用Xpath或Css选择器获取特定的图像,我可以获取一个图像,但我希望一次获取所有图像(7个URL),并将它们放入列表中 有什么建议吗 这是我的密码: from selenium import webdriver from selenium.webdriv

我以前从其他Web元素中提取过“src”,但这一个似乎让我难以理解。我正试图抓住所有的链接到这个网页上的所有图像。然而,即使webelement由css选择器高亮显示,当我试图通过get_属性(“src”)获取它时,它也不会返回链接。如果我使用Xpath或Css选择器获取特定的图像,我可以获取一个图像,但我希望一次获取所有图像(7个URL),并将它们放入列表中

有什么建议吗

这是我的密码:


from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.action_chains import ActionChains
from json import dumps, loads, JSONEncoder, JSONDecoder

import re
import json
import timeit
import time

start = timeit.default_timer()

driver = webdriver.Chrome()

wait = WebDriverWait(driver, 5)



def ImageFind():
    driver.get('https://shop.freedommobile.ca/devices/Apple/iPhone_11?sku=190199220546&planSku=Freedom%205GB')


    phoneImage = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.slide')))
    imageLink = phoneImage[0].get_attribute('src') #test for grabbing url for just one webelement

    print(len(phoneImage)) #check to make sure I have the right amount of elements being selected
    print(phoneImage) 
    print(imageLink)

    return imageLink


ImageFind()



您使用了错误的选择器,
.slide
li元素,并且没有src属性。要获取图像,请使用
。幻灯片img
,如下例所示:

images = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.slide img')))
for image in images:
    print(image.get_attribute('src'))
您的方法:

def image_find():
    driver.get('https://shop.freedommobile.ca/devices/Apple/iPhone_11?sku=190199220546&planSku=Freedom%205GB')

    images = [image.get_attribute('src') for image in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.slide img')))]
    return images