我已经try 过多次使用教程中的其他指令代码,这些代码都很好.然而,当我刚刚更改了URL和下面的类时,它会给出一个错误:

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
  (Session info: chrome=100.0.4896.88)

当我使用教程代码时,一切都正常.

这是我的代码(我已经从互联网上解决了一些chrome驱动程序问题)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options)

driver.get("https://raritysniper.com/nft-drops-calendar")

time.sleep(1)

link = driver.find_element_by_css_selector(".w-full.h-full.align-middle.object-cover.dark:brightness-80.dark:contrast-103.svelte-f3nlpp").get_attribute("alt")

print(link)

我正在try 获取每个项目的属性,并将它们转换为csv.

Screen shot of HTML that I am trying to extract

如果有人能描述我在代码上遇到的问题,那就太好了.

非常感谢.

推荐答案

你正在使用的CSS_SELECTOR

.w-full.h-full.align-middle.object-cover.dark:brightness-80.dark:contrast-103.svelte-f3nlpp

与HTML中的任何元素都不匹配.

相反,你应该使用CSS_SELECTOR:

div.w-full.h-full.align-middle img:not(.placeholder)

代码:

driver.maximize_window()

wait = WebDriverWait(driver, 30)

driver.get("https://raritysniper.com/nft-drops-calendar")

#time.sleep(1)

first_element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.w-full.h-full.align-middle img:not(.placeholder)")))
print(first_element.get_attribute('alt'))
print(first_element.get_attribute('src'))

Import:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Output:

NEON PLEXUS
https://media.raritysniper.com/featured/neon-plexus_1648840196045_3.webp

Process finished with exit code 0

Python相关问答推荐

Pandas :多索引组

对Numpy函数进行载体化

Pandas 在最近的日期合并,考虑到破产

Django mysql图标不适用于小 case

在Polars(Python库)中将二进制转换为具有非UTF-8字符的字符串变量

发生异常:TclMessage命令名称无效.!listbox"

按顺序合并2个词典列表

Python虚拟环境的轻量级使用

对所有子图应用相同的轴格式

根据列值添加时区

提取相关行的最快方法—pandas

pandas:对多级列框架的列进行排序/重新排序

以逻辑方式获取自己的pyproject.toml依赖项

如何在海上配对图中使某些标记周围的黑色边框

一个telegram 机器人应该发送一个测验如何做?""

如何在验证文本列表时使正则表达式无序?

获取git修订版中每个文件的最后修改时间的最有效方法是什么?

ModuleNotFoundError:Python中没有名为google的模块''

Seaborn散点图使用多个不同的标记而不是点

我怎么才能用拉夫分拣呢?