简体   繁体   English

如何使用带有Python的Selenium Webdriver从工具提示中获取没有属性的文本?

[英]How to get text from tooltip with no attributes using Selenium Webdriver with Python?

I have a web element with a tooltip that shows the following message: ● Client Book Revenue $20,966,618 我有一个带有工具提示的Web元素,其中显示以下消息:●客户账簿收入$ 20,966,618

The HTML code for that tooltip is below. 该工具提示的HTML代码如下。 I am able to hover over the web element using Selenium Webdriver which makes the tooltip visible, but I can't figure out how to get the text from it. 我可以使用Selenium Webdriver将鼠标悬停在web元素上,这使工具提示可见,但是我不知道如何从中获取文本。 Could somebody please help? 有人可以帮忙吗?

<div class="highcharts-tooltip" style="position: absolute; left: 755px; top: 0px; display: block; opacity: 1; pointer-events: none; visibility: visible;">
    <span style="position: absolute; font-family: "Roboto",sans-serif; font-size: 12px; white-space: nowrap; color: rgb(51, 51, 51); margin-left: 0px; margin-top: 0px; left: 0px; top: 0px;">
        <div class="client-rate-bench-chart">
            <table class="table rdo-table-tooltip">
                <tbody>
                    <tr>
                        <td>
                            <span style="color:rgba(45,108,162,1)">●</span>
                           Client Book Revenue
                        </td>
                        <td> $20,966,618 </td>
                    </tr>
                </tbody>
           </table>
        </div>
    </span>
</div>

You can grab the table and then grab the first instance of <tr> 您可以获取表,然后获取<tr>的第一个实例

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Firefox()
driver.get(URL)
html = driver.page_source # this is how you get the HTML

soup = BeautifulSoup(html)
table = soup.find('table', class_='rdo-table-tooltip')
tooltip = table.find('tr')
text = tooltip.text

text will have a lot of extra whitespace because of how the HTML is formatted, but you can strip that out - just split on all whitespace and then re-join the elements like this 由于HTML的格式设置, text将有很多额外的空格,但是您可以将其删除-只需在所有空格上分割,然后重新加入这样的元素

final_text = ' '.join(text.split())
print final_text
# ● Client Book Revenue $20,966,618

For multiple <tr> s you can use .find_all('tr') and then use a list comprehension to get a list of the contents of the rows. 对于多个<tr> ,可以使用.find_all('tr') ,然后使用列表.find_all('tr')来获取行内容的列表。 It would look something like this 看起来像这样

soup = BeautifulSoup(html)
table = soup.find('table', class_='rdo-table-tooltip')
tooltips = table.find_all('tr')
text = [' '.join(tooltip.text.split()) for tooltip in tooltips]

Then text will be a list of strings containing the text from each <tr> 然后,文本将是一个字符串列表,其中包含每个<tr>中的文本

As an alternative you could use re.findall to return all the instances of text between tags. 或者,您可以使用re.findall返回标签之间的所有文本实例。 This will involve some cleaning up afterwards but I have found it pretty handy in general when working with Selenium. 此后将需要进行一些清理,但是我发现在使用Selenium时通常非常方便。

import re

tooltips = re.findall('<tr>(.*?)<tr>', html.replace('\n', ''))

for tooltip in tooltips:
    print tooltip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium WebDriver 和 Python 从表中获取元素(文本) - How to get element(text) from a table using Selenium WebDriver with Python 如何使用 selenium 和 python 从网站获取工具提示文本,其中文本来自 javascript - How can I get the tooltip text from a website using selenium and python where the text comes from a javascript 使用Python和Selenium Webdriver从textarea获取文本(无值属性) - Get text from textarea using Python & Selenium Webdriver (no value attribute) 如何使用Python使用Selenium Webdriver获取此html中的一些文本值? - How to get the some text value in this html with Selenium Webdriver using Python? 如何使用selenium webdriver python中的span中的定位器打印文本? - How to print the text using a locator from the span in selenium webdriver python? 如何在 For 循环 Python 中使用 Selenium WebDriver 获取文本 - How to get text with Selenium WebDriver in a For Loop Python 如何在 Python 中使用 Selenium WebDriver 获取文本 - How to get text with Selenium WebDriver in Python 使用 Selenium WebDriver 获取文本 - Get Text By Using Selenium WebDriver 如何使用 Selenium WebDriver 获取文本? - How to get text with Selenium WebDriver? 如何在 python selenium webdriver 中获取 div 属性文本 - how to get div properties text in python selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM