简体   繁体   中英

Alter XPath to Extract Text Selenium

This HTML block:

<td class="tl-cell tl-popularity" data-tooltip="9,043,725 plays" data-tooltip-instant="">
  <div class="pop-meter">
  <div class="pop-meter-background"></div>
  <div class="pop-meter-overlay" style="width: 57%"></div>
  </div>
</td>

equates to this XPath:

xpath = '//*[@id="album-tracks"]/table/tbody/tr[5]/td[6]'

Trying to extract the text: 9,043,725 plays with

find_element_by_xpath(xpath).text()

returns an empty string. This text is only generated when a user hovers their mouse over the HTML block.

Is there a way to alter the XPath so that an empty string is not returned but the actual string is returned?

Try using get_attribute instead. The intended element can be located using any find_elements mechanisms. See the API DOC

element = browser.find_elements_by_css_selector('.tl-cell.tl-popularity')
text = element.get_attribute('data-tooltip')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM