Home > Net >  Get a value from aria label using python selenium
Get a value from aria label using python selenium

Time:02-01

Am trying to get Aria label value Hi Guys am trying to get this value "021" from this line of code but am not getting it , i only getting None as a return My so far code enter image description here enter image description here

could you guys , pls help me out here

Hi Guys am trying to get this value "021" from this line of code but am not getting it , i only getting None as a return My so far code enter image description here enter image description here

could you guys , pls help me out here

CodePudding user response:

You need to set the xpath for the bdi element and not the span element. The bdi element contains the text 021 that you want to extract

It should be

byy = driver.find_element(By.XPATH,"//*[@id='__label12-bdi']")

CodePudding user response:

As per the given HTML:

HTML

You can extract 021 either from the <span> tag or from the descendant <bdi> tag.


Solution

To print 021 you have you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using <span> tag and aria-label attribute:

    • Using CSS_SELECTOR:

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span#__label12"))).get_attribute("aria-label"))
      
    • Using XPATH:

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@id='__label12']"))).get_attribute("aria-label"))
      
  • Using <bdi> tag and text attribute:

    • Using CSS_SELECTOR:

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span#__label12 bdi#__label12-bdi"))).text)
      
    • Using XPATH:

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@id='__label12']/bdi[@id='__label12-bdi']"))).text)
      
  • Note : You have to add the following imports :

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