0

This is the HTML. I want to retrieve the value Unblock from the element in the 4th line using selenium webdriver in python. Please help me regarding the same.

<a class="btn btn-micro hasTooltip" 
    href="javascript:void(0);" 
    onclick="return listItemTask('cb0','users.unblock')" 
    title="" data-original-title="Unblock">
    <span class="icon-unpublish">
    </span>
</a>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Farhaan Patel
  • 59
  • 1
  • 5

3 Answers3

0

Use this code

driver.findElement_by_xpath("//a[@class='btn btn-micro hasTooltip']").get_attribute("data-original-title");
Rajagopalan
  • 5,465
  • 2
  • 11
  • 29
0

If you want to print attribute value in an element then you have to use getAttribute() method. You can use this code, you can convert it into Python as it is in JAVA

WebElement eletext= driver.findElement(By.xpath("//a[@class='btn btn-micro hasTooltip']"));

String textElement = eletext.getAttribute("data-original-title");    
System.out.println(textElement);

Difference:

getAttribute() - It extracts the text of any attribute in the HTML tag.

getText() - It extracts the innerText of a WebElement.

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
0

As per the HTML you have shared the desired element is a React element so you have to induce WebDriverWait for the element to be visible and you can use either of the following solution:

  • CSS_SELECTOR:

    myText = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.btn.btn-micro.hasTooltip[onclick*='users.unblock']"))).get_attribute("data-original-title")
    
  • XPATH:

    myText = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='btn btn-micro hasTooltip'][contains(@onclick,'users.unblock')]"))).get_attribute("data-original-title")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352