Home > Software design >  Selenium - cant find element within element
Selenium - cant find element within element

Time:08-11

I do not understand why Selenium does not allow me to search for elements within elements. I want to create a new element called property that contains the subset of data I would like to obtain from 'main' which I extracted from the website. I heard Selenium decented find_element so I am unsure what else to use here.

website for reference: https://www.hellolanding.com/s/austin-tx/apartments/furnished

from ast import Try
from gettext import find
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome(executable_path="C:/Users/Carson/Desktop/chromedriver.exe")
driver.get("https://www.hellolanding.com/s/austin-tx/apartments/furnished")

try:
    main = WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.ID,'search-home-list-wrapper'))
    )
    property = main.find_elements(By.CLASS_NAME, 'transition-delay[200ms] relative flex flex-col mb-6 w-full overflow-hidden e1mngczp3 rounded-t-lg css-12puc69 e4dpi0y0')
    
    print(property.text)
    print("successsssssssssssssssssssssssssssss")
    driver.quit()

except:

    driver.quit()
    print("failed")

CodePudding user response:

You are doing find_elements() which returns a list of elements, not just 1. So your call of property.text will raise an exception. You print "fail" instead of the error so it hurts when debugging

You have an invalid class with the long name. Just try to use hte common one like "e1mngczp3"

properties = main.find_elements(By.CLASS_NAME, 'e1mngczp3')
for property in properties:
    print(property.text)
  • Related