Home > Software engineering >  I want to get text from "aria-label" by python selenium
I want to get text from "aria-label" by python selenium

Time:12-24

try:
    Rating = driver.find_elements(By.CSS_SELECTOR,'div.b5cd09854e.d10a6220b4')
except:
    Rating = None
Rating1 = []
for i in Rating:
    store = {
        'Rating': i.get_attribute("[aria-label]")
    }
    Rating1.append(store)
print(Rating1)

Output

<selenium.webdriver.remote.webelement.WebElement (session="867a6f4433baacd665edd311671faa81", element="69d53b22-5d2e-4e1a-8f55-682affc043b1")>

I want scrape data from a website but I can't extract "aria-label" web elements text by python selenium. please experts help me to extract "aria-label" web elements to get text from it. fix it and rewrite

CodePudding user response:

so to get hold of the text you just use the .text.

Like so:-

Rating = driver.find_elements(By.CSS_SELECTOR,'div.b5cd09854e.d10a6220b4')
    
for i in Rating:
   print(i.text)

Hope this helps :)

CodePudding user response:

just use () instead of also using [] inside ().

So your code should be like this. .

try:
    Rating =driver.find_elements(By.CSS_SELECTOR,'div.b5cd09854e.d10a6220b4')
except:
    Rating = None
Rating1 = []
for i in Rating:
    store = {
        'Rating': i.get_attribute("aria-label")
    }
    Rating1.append(store)
print(Rating1)
  • Related