Page 1 of 1

Web Scraping proplem kein solches Element

Posted: 26 Aug 2025, 08:58
by Anonymous
Ich versuche, Web -Scraping mit Selenium zu machen. Ich habe ein Problem mit der folgenden Nachricht,
Dieses Problem bezieht sich auf kein Element wie das, was es bedeutet? < /p>

Code: Select all

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"a[1]/div[1]/img"}
(Session info: chrome=117.0.5938.149); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
< /code>
Und ich habe auch immer mehr gesucht und es nicht gelöst. Kann mir jemand helfen?from selenium import webdriver
import time as t
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

import urllib
import cv2
import os

try:
os.mkdir("G:/Smokking_Project")
except:
pass

name="smoked"

chrome_options = webdriver.ChromeOptions()

chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

#driver = webdriver.Chrome(executable_path='chromedriver.exe',options=chrome_options)
driver = webdriver.Chrome(options=chrome_options)

strr="https://www.google.com/search?q=smokinng&tbm=isch&ved=2ahUKEwi8k9zn9eOBAxVtlycCHTa_DnUQ2-cCegQIABAA&oq=smokinng&gs_lcp=CgNpbWcQAzIJCAAQGBCABBAKMgkIABAYEIAEEAoyCQgAEBgQgAQQCjoECCMQJzoFCAAQgAQ6BggAEAUQHjoECAAQHjoICAAQgAQQsQM6BAgAEAM6BwgAEBgQgARQjwdY8xJg-RloAHAAeACAAb0BiAHsCZIBAzAuOZgBAKABAaoBC2d3cy13aXotaW1nwAEB&sclient=img&ei=uUwhZfzSFO2unsEPtv66qAc&bih=723&biw=1517&hl=en"

driver.get(strr)
t.sleep(3)

pics= driver.find_element(By.XPATH,'//*[@id="islrg"]/div[1]')
links=[]
x=1
last_height=0

def download_image(url,filename):
resource = urllib.request.urlopen(url)
output = open(filename,"wb")
output.write(resource.read())
output.close()

while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
t.sleep(4)
pics_ =pics.find_elements(By.XPATH,'./*')

for pic in pics_:
#try:
#img_link=pic.find_element_by_xpath('a[1]/div[1]/img').get_attribute('src')
#img_link=pic.find_element_by_xpath('a[1]/div[1]/img').get_attribute('src')
img_link= driver.find_element(By.XPATH,'a[1]/div[1]/img').get_attribute('src')

if img_link not in links:
links.append(img_link)

try:
os.mkdir('G://Smokking_Project//'+name)
except:
pass
try:
os.mkdir('G://Smokking_Project//'+name)
except:
pass
file_name='Smokking_Project//'+name+'//'+str(x)+'.jpg'
download_image(img_link,file_name)

x+=1
#except:
#  print('-',end='')
new_height = driver.execute_script("return document.body.scrollHeight")
print(new_height)
if new_height == last_height:
break
last_height = new_height

driver.close()
Danke