7

I have the following HTML span:

<button class="coreSpriteHeartOpen oF4XW dCJp8">
    <span class="glyphsSpriteHeart__filled__24__red_5 u-__7" aria-label="Unlike"></span>
</button>

I also have a webElement representing the button containing this span that I have found using xpath. How can I retrieve the aria-label value (Unlike) from the element?

I tried to do:

btn = drive.find_element(By.xpath, "xpath") 
btn.get_attribute("aria-label")

but it returns nothing. How to retrieve the text value of an element with 'aria-label' attribute from the element object?

Vishal Kharde
  • 1,553
  • 3
  • 16
  • 34
ben
  • 1,064
  • 3
  • 15
  • 29
  • 1
    Can you post a more complete code/document example with the containing button and the code that creates `btn` in Python? – ggorlen Aug 24 '18 at 18:20
  • Is it enough now? – ben Aug 24 '18 at 18:33
  • That helps, but how did you start the driver? I should have linked [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve), otherwise I need to make assumptions about your code that may not be accurate. Even so, `"xpath"` looks like a pretty suspicious xpath to me. – ggorlen Aug 24 '18 at 18:41
  • 1
    share xpath locator you used – Sers Aug 24 '18 at 20:40
  • Could you explain to me how Instagram js detect which like button you clicked since in one page there are multiple like buttons with same class name ? – user1788736 Jul 12 '19 at 21:01

5 Answers5

9

aria-label is attribute of span element, not button. You can get it like this:

btn = drive.find_element(By.xpath, "xpath") 
aria_label = btn.find_element_by_css_selector('span').get_attribute("aria-label")

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 need
btn = drive.find_element(By.XPATH, '//button[./span[@aria-label="Unlike"] and contains(@class,"coreSpriteHeartOpen)]')
Sers
  • 12,047
  • 2
  • 12
  • 31
1

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"));
Vishal Kharde
  • 1,553
  • 3
  • 16
  • 34
0

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.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0
# Like method
lov = el.find_element_by_class_name('glyphsSpriteHeart__outline__24__grey_9').click()  

# Unlike method
lov = el.find_element_by_class_name('glyphsSpriteHeart__filled__24__red_5').click() 

I used these methods instead and it worked!

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
0

If xpath seems complicated, you can retrieve it with the class name and use the get_attribute method to access it.

value = driver.find_element(By.CLASS_NAME, 'glyphsSpriteHeart__filled__24__red_5 u-__7')
output = value.get_attribute("aria-label")