Home > database >  selenium find_element_by_css_selector for the long class name
selenium find_element_by_css_selector for the long class name

Time:04-15

I have tried multiple times with other instruction codes with space from the tutorial which worked fine. However, when I just changed the URL and the following class, it would give the error saying

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

Everything worked when I used the tutorial code.

Here is my code (I have solved a few chrome driver problems from the internet)

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)

I am trying to get attributes of each projects and make them into csv. (Please refer to the screenshot)

Screen shot of HTML that I am trying to extract

it would be wonderful if anyone could depict the problem I got with the code.

Thank you!

CodePudding user response:

The CSS_SELECTOR that you are using

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

does not really match any element in the HTML.

Instead, you should use this CSS_SELECTOR:

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

In code:

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
  • Related