简体   繁体   English

Selenium Webdriver如何从鼠标悬停元素中获取文本?

[英]How can Selenium Webdriver get text from mouseover element?

I am trying to scrape my discord server using selenium to search for messages and get the details of the message, specifically the date and time the messages were sent.我正在尝试使用 selenium 搜索我的 discord 服务器来搜索消息并获取消息的详细信息,特别是发送消息的日期和时间。

I have been able to get the time from the text contents of the message element, however the date is behind a mouseover hover tooltip (if you have used discord you might know what I mean).我已经能够从消息元素的文本内容中获取时间,但是日期位于鼠标悬停 hover 工具提示之后(如果您使用过 discord,您可能知道我的意思)。

I've tried to search for the way to get the text of the tooltip but none of the ways have worked/the solutions available were not about getting the text.我试图寻找获取工具提示文本的方法,但没有一种方法有效/可用的解决方案与获取文本无关。 ie get_attribute('title') doesn't work.get_attribute('title')不起作用。

For reference the element of the tooltip is:作为参考,工具提示的元素是:

<span class="latin12CompactTimeStamp-38a8OU timestamp-3ZCmNB timestampVisibleOnHover-2bQeI4 alt-1uNpEt"><span aria-label="15:03"><i class="separator-2nZzUB" aria-hidden="true">[</i>15:03<i class="separator-2nZzUB" aria-hidden="true">] </i></span></span>

I can edit and include my python code but I don't think it would help to explain much.我可以编辑并包含我的 python 代码,但我认为这对解释没有太大帮助。

在此处输入图像描述

url is in the form ( https://discord.com/channels/{guild ID}/{channel ID}/{message ID}) to my knowledge...据我所知,url 的格式为( https://discord.com/channels/{guild ID}/{channel ID}/{message ID}) ...

The date is in the aria-label in the span child class日期在跨度子 class 的 aria-label 中

elem=driver.find_element_by_xpath("//span[starts-with(@class,'latin12CompactTimeStamp')]/span[1]")

Then use either然后使用

print(elem.get_attribute('aria-label'))
print(elem.text)

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

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