Home > Blockchain >  Why is Selnium not able to find element by class?
Why is Selnium not able to find element by class?

Time:06-13

I was trying to extract some data from the website but the code below just returns a empty list when there is clearly an element by the class name given in the code.

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://terraria.fandom.com/wiki/Weapons")
search = driver.find_elements(By.CLASS_NAME, "infocard clearfix terraria compact")
print(search)

I am really puzzled, I even tried using sleep to give the website time to load but it didn't affect the result. Any help will be much appreciated.

CodePudding user response:

You are getting an empty data item because infocard clearfix terraria compact did't select any data point. The following element selector/locator strategy is working.

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
#chrome to stay open to see what's happening in the real word or make it comment to close
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)   
URL ='https://terraria.fandom.com/wiki/Weapons'
driver.get(URL)
time.sleep(5)

for item in driver.find_elements(By.XPATH,'//*[@]/ul/li/span/span/span/a'):
    print(item.text)

Output:

Copper Shortsword
Tin Shortsword
Wooden Sword
Boreal Wood Sword
Copper Broadsword
Iron Shortsword
Palm Wood Sword
Rich Mahogany Sword
Cactus Sword
Lead Shortsword
Silver Shortsword
Tin Broadsword
Ebonwood Sword
Iron Broadsword
Shadewood Sword
Tungsten Shortsword
Gold Shortsword
Lead Broadsword
Silver Broadsword
Bladed Glove
Tungsten Broadsword
Zombie Arm
Gold Broadsword
Platinum Shortsword
Mandible Blade
Stylish Scissors
Ruler
Platinum Broadsword
Umbrella
Breathing Reed
Gladius
Bone Sword
Bat Bat
Tentacle Spike
Candy Cane Sword
Katana
Ice Blade
Light's Bane
Tragic Umbrella
Muramasa
Exotic Scimitar
Phaseblade
Blood Butcherer
Starfury
Enchanted Sword
Purple Clubberfish
Bee Keeper
Falcon Blade
Blade of Grass
Fiery Greatsword
Night's Edge
Pearlwood Sword
Classy Cane
Slap Hand
Cobalt Sword
Palladium Sword
Phasesaber
Ice Sickle
Brand of the Inferno
Mythril Sword
Orichalcum Sword
Breaker Blade
Cutlass
Frostbrand
Adamantite Sword
Beam Sword
Titanium Sword
Fetid Baghnakhs
Bladetongue
Tizona
Ham Bat
Excalibur
True Excalibur
Chlorophyte Saber
Death Sickle
Psycho Knife
Keybrand
Chlorophyte Claymore
The Horseman's Blade
Christmas Tree Sword
True Night's Edge
Seedler
Flying Dragon
Starlight
Terra Blade
Influx Waver
Star Wrath
Meowmere
Wooden Yoyo
Rally
Malaise
Artery
Amazon
Code 1
Valor
Cascade
Format:C
Gradient
Chik
Hel-Fire
Amarok
Code 2
Yelets
Red's Throw
Valkyrie Yoyo
Kraken
The Eye of Cthulhu
Terrarian
Spear
Trident
Storm Spear
The Rotted Fork
Swordfish
Dark Lance
Cobalt Naginata
Palladium Pike
Mythril Halberd
Orichalcum Halberd
Adamantite Glaive
Titanium Trident
Gungnir
Ghastly Glaive
Chlorophyte Partisan
Tonbogiri
Mushroom Spear
Obsidian Swordfish
North Pole
Wooden Boomerang
Enchanted Boomerang
Fruitcake Chakram
Bloody Machete
Shroomerang
Ice Boomerang
Thorn Chakram
Combat Wrench
Flamarang
Flying Knife
Sergeant United Shield
Light Disc
Bananarang
Possessed Hatchet
Paladin's Hammer
Chain Knife
Mace
Flaming Mace
Ball O' Hurt
The Meatball
Blue Moon
Sunfury
Anchor
KO Cannon
Drippler Crippler
Chain Guillotines
Dao of Pow
Flower Pow
Golem Fist
Flairon
Terragrim
Arkhalis
Jousting Lance
Shadowflame Knife
Hallowed Jousting Lance
Sleepy Octopod
Scourge of the Corruptor
Shadow Jousting Lance
Vampire Knives
Sky Dragon's Fury
Daybreak
Solar Eruption
Zenith
Wooden Bow
Boreal Wood Bow
Copper Bow
Palm Wood Bow
Rich Mahogany Bow
Tin Bow
Ebonwood Bow
Iron Bow
Shadewood Bow
Lead Bow
Silver Bow
Tungsten Bow
Gold Bow
Platinum Bow
Demon Bow
Tendon Bow
Blood Rain Bow
The Bee's Knees
Hellwing Bow
Molten Fury
Sharanga
Pearlwood Bow
Marrow
Ice Bow
Daedalus Stormbow
Shadowflame Bow
Phantom Phoenix
Pulse Bow
Aerial Bane
Tsunami
Eventide
Phantasm
Cobalt Repeater
Palladium Repeater
Mythril Repeater
Orichalcum Repeater
Adamantite Repeater
Titanium Repeater
Hallowed Repeater
Vulcan Repeater
Chlorophyte Shotbow
Stake Launcher
Red Ryder
Flintlock Pistol
Musket
The Undertaker
Sandgun
Revolver
Minishark
Boomstick
Quad-Barrel Shotgun
Handgun
Phoenix Blaster
Pew-matic Horn
Clockwork Assault Rifle
Gatligator
Shotgun
Onyx Blaster
Coin Gun
Uzi
Megashark
Venus Magnum
Tactical Shotgun
Sniper Rifle
Candy Corn Rifle
Chain Gun
Xenopopper
Vortex Beater
S.D.M.G.
Grenade Launcher
Proximity Mine Launcher
Rocket Launcher
Nail Gun
Stynger
Jack 'O Lantern Launcher
Snowman Cannon
Celebration
Electrosphere Launcher
Celebration Mk2
Paper Airplane
White Paper Airplane
Shuriken
Throwing Knife
Poisoned Knife
Snowball
Spiky Ball
Bone
Rotten Egg
Star Anise
Molotov Cocktail
Frost Daggerfish
Javelin
Bone Javelin
Bone Throwing Knife
Grenade
Sticky Grenade
Bouncy Grenade
Beenade
Happy Grenade
Flare Gun
Ale Tosser
Blowpipe
Blowgun
Snowball Cannon
Paintball Gun
Harpoon
Egg Cannon
Star Cannon
Toxikarp
Dart Pistol
Dart Rifle
Flamethrower
Piranha Gun
Elf Melter
Super Star Shooter
Wand of Sparking
Thunder Zapper
Amethyst Staff
Topaz Staff
Sapphire Staff
Emerald Staff
Ruby Staff
Diamond Staff
Amber Staff
Vilethorn
Weather Pain
Magic Missile
Aqua Scepter
Flower of Fire
Flamelash
Sky Fracture
Crystal Serpent
Flower of Frost
Frost Staff
Crystal Vile Shard
Life Drain
Meteor Staff
Poison Staff
Rainbow Rod
Unholy Trident
Tome of Infinite Wisdom
Venom Staff
Nettle Burst
Bat Scepter
Blizzard Staff
Inferno Fork
Shadowbeam Staff
Spectre Staff
Resonance Scepter
Razorpine
Staff of Earth
Betsy's Wrath
Gray Zapinator
Space Gun
Bee Gun
Roman candle
Laser Rifle
Orange Zapinator
Zapinator
Wasp Gun
Leaf Blower
Rainbow Gun
Heat Ray
Laser Machinegun
Charged Blaster Cannon
Bubble Gun
Water Bolt
Book of Skulls
Demon Scythe
Cursed Flames
Golden Shower
Crystal Storm
Magnet Sphere
Razorblade Typhoon
Lunar Flare
Crimson Rod
Ice Rod
Clinger Staff
Nimbus Rod
Magic Dagger
Medusa Head
Spirit Flame
Shadowflame Hex Doll
Blood Thorn
Magical Harp
Toxic Flask
Nightglow
Stellar Tune
Nebula Arcanum
Nebula Blaze
Last Prism
Finch Staff
Slime Staff
Flinx Staff
Abigail's Flower
Hornet Staff
Vampire Frog Staff
Imp Staff
Blade Staff
Spider Staff
Pirate Staff
Sanguine Staff
Optic Staff
Deadly Sphere Staff
Pygmy Staff
Raven Staff
Desert Tiger Staff
Tempest Staff
Terraprisma
Xeno Staff
Stardust Cell Staff
Stardust Dragon Staff
Lightning Aura Rod
Flameburst Rod
Explosive Trap Rod
Ballista Rod
Houndius Shootius
Queen Spider Staff
Lightning Aura Cane
Flameburst Cane
Explosive Trap Cane
Ballista Cane
Staff of the Frost Hydra
Lightning Aura Staff
Flameburst Staff
Explosive Trap Staff
Ballista Staff
Lunar Portal Staff
Rainbow Crystal Staff
Leather Whip
Snapthorn
Spinal Tap
Firecracker
Cool Whip
Durendal
Dark Harvest
Morning Star
Kaleidoscope
Snowball Launcher
Cannon
Bunny Cannon
Land Mine
Bomb
Sticky Bomb
Bouncy Bomb
Scarab Bomb
Dynamite
Sticky Dynamite
Bouncy Dynamite
Bomb Fish
Holy Hand Grenade
Holy Water
Unholy Water
Blood Water
Boring Bow
Etherian Javelin
First Fractal
Skull Bow
Scythe
Soul Scythe
Icemourne

WebdriverManager

  • Related