Search code examples
seleniumselenium-webdriverxpathgetattribute

i want python xpath data-tip print


I want to print the "data-tip" data here. but I couldn't do it anyway.

<div class="rightstats" id="my_cash"><div data-number="3044519" data-positions="6" class="val" data-val="2044519" data-tip="2&amp;nbsp;044&amp;nbsp;519 cash" data-mobiletip="1&amp;nbsp;973&amp;nbsp;762 cash">2&nbsp;M</div><div class="plus" onclick="show_dialog('shop_treasures');"></div></div>
cash= site.find_element("xpath","//*\[@id='my_cash'\]/div\[1\]")
time.sleep(1)
print ('cash= ', cash.text)

outputs

"2m"

I tried

cash= site.find_element("xpath","//*\[@id='my_cash'\]/div\[1\]/@data-tip")
time.sleep(1)
print ('cash= ', cash.text)

I want to print the "data-tip" data here. but I couldn't do it anyway.


Solution

  • data-tip is not a text content but an attribute, so instead of cash.text you should apply cash.get_attribute("data-tip") here.
    The entire code could be:

    cash= site.find_element(By.XPATH,"//*[@id='my_cash']/div[1]")
    print('cash= ', cash.get_attribute("data-tip"))