Skip to content Skip to sidebar Skip to footer

How To Retrieve The Value Of The Attribute Aria-label From Element Found Using Xpath As Per The Html Using Selenium

I have the following HTML span:

Or if your goal is to find button with span contains attribute aria-label="Unlike":

btn = drive.find_element(By.XPATH, '//button[./span[@aria-label="Unlike"]]')
#you can add class to xpath also if you needbtn = drive.find_element(By.XPATH, '//button[./span[@aria-label="Unlike"] and contains(@class,"coreSpriteHeartOpen)]')

Solution 2:

Following worked for me in Java,

WebElement btnelement= driver.findElement(
                        By.xpath("//span[@aria-label='Unlike']"));
System.out.println("Attribute value is " + btnelement.getAttribute("value"));

Solution 3:

As per your question and the HTML you have shared it seems that the element is a React element, so to retrieve the attribute aria-label you have to induve WebDriverWait for the desired element to be visible and you can use the following solution:

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "element_xpath_you_found"))).get_attribute("aria-label"))

Note : You have to add the following imports :

from selenium.webdriver.support.uiimportWebDriverWaitfrom selenium.webdriver.common.byimportByfrom selenium.webdriver.supportimport expected_conditions asEC

Solution 4:

# Like methodlov = el.find_element_by_class_name('glyphsSpriteHeart__outline__24__grey_9').click()  

# Unlike methodlov = el.find_element_by_class_name('glyphsSpriteHeart__filled__24__red_5').click() 

I used these methods instead and it worked!

Post a Comment for "How To Retrieve The Value Of The Attribute Aria-label From Element Found Using Xpath As Per The Html Using Selenium"