当元素存在时硒找不到元素

Selenium can not find a element when elemnt exist

你好,我不喜欢 API,它既无聊又难用,所以我使用 selenium 从 artstation.com 下载图像到我的电脑,我使用这个脚本:

artist = random.choice(["haiyu","cgnox"])
driver.get(f"https://www.artstation.com/{artist}")
time.sleep(4)
image = driver.find_element(By.XPATH,"/html/body/div[3]/div[2]/div/div/div[1]/auto-scrollable-to-top-on-location-change/user-projects[1]/div/div/div[1]/a/div")
image = image.find_element(By.XPATH,"/html/body/div[3]/div[3]/div/div/div[1]/auto-scrollable-to-top-on-location-change/user-projects[1]/div")

print(image)

但是 selenium 总是给我带来这个错误:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: 

/html/body/div[3]/div[3]/div/div/div[1]/auto-scrollable-to-top-on-location-change/user-projects[1]/div
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:183:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:395:5
element.find/</<@chrome://remote/content/marionette/element.js:300:16

我不知道问题出在哪里,因为我从浏览器复制了一个元素 顺便说一句,我真正想要的是 select 图库 class.

中的 src 之一

似乎 xpath 选择不正确,我在列表中使用关键字作为 for 循环。现在一切正常。

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

artists = ["haiyu","cgnox"]
for artist in artists:
    url=f"https://www.artstation.com/{artist}"
    print(url)
    driver.get(url)
    driver.maximize_window()
    time.sleep(5)
    image=[im.get_attribute('src')for im in driver.find_elements(By.XPATH,'//*[@class="project artist-profile"]/a/div/div/img')]
    print(image)

输出:

['https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595', 'https://cdna.artstation.com/p/users/avatars/000/023/520/medium/a627c2c5e1ba583e0243be0e2d8abfbf.jpg?1647858595',

...等等