简体   繁体   English

如何使用 selenium 和 python 从网站获取工具提示文本,其中文本来自 javascript

[英]How can I get the tooltip text from a website using selenium and python where the text comes from a javascript

I am trying to get the tooltip text that shows when I hover over how long ago the game was played https://www.leagueofgraphs.com/summoner/eune/AnFrey99我正在尝试获取工具提示文本,该文本显示我 hover 多久以前玩过游戏https://www.leagueofgraphs.com/summoner/eune/AnFrey99在此处输入图像描述 ] 1 ] 1

In the html code it doesn't shows the text and I figure it comes from a javascript and each rows has a script inside the tr tag, but so far I was not able to get the value of the var newTooltipData.在 html 代码中,它不显示文本,我认为它来自 javascript,每行在 tr 标签内都有一个脚本,但到目前为止我无法获得 var newTooltipData 的值。


    var newTooltipData = {"match-2733966814": (new Date(1612964002882).toLocaleDateString() + " " + new Date(1612964002882).toLocaleTimeString()) + " - 31min 48s"};
    if (window.tooltipData) {
        window.tooltipData = Object.assign(window.tooltipData, newTooltipData);
    } else {
        window.tooltipData = newTooltipData;
    }

I want to get the exact date for each row along with other information that I already done. 我想获得每一行的确切日期以及我已经完成的其他信息。 Here is my code and my tries. 这是我的代码和我的尝试。
 driver_second = webdriver.Firefox(executable_path=DRIVER_PATH) driver_second.get("https://www.leagueofgraphs.com/summoner/eune/"+'AnFrey99') time.sleep(5) accept_button=driver_second.find_element_by_xpath("/html/body/div[3]/div/div/div[3]/div[1]/button[2]") accept_button.click() tabel=driver_second.find_element_by_xpath("//table[@class='data_table relative recentGamesTable']") rows=tabel.find_elements_by_xpath("tbody/tr") for row in rows: elements=row.find_elements_by_xpath("td") if(len(elements)>1): script=row.find_element_by_xpath("script") data=elements[2].find_element_by_xpath("a/div[3]") data=script.get_property('innerHTML')

If you hover the div, JavaScript will append a new div to the website with the ID 'tooltip' (full source of the function here ): If you hover the div, JavaScript will append a new div to the website with the ID 'tooltip' (full source of the function here ):

var TooltipManager = (function () {
    [...]
            var tooltipElement = $('#tooltip');
            if (!tooltipElement.length) {
                $('body').append('<div id="tooltip"></div>'); #New Div appended
                [...]

Therefore Selenium can find this new div id = "tooltip" after you hover over the div, which calls this function.因此 Selenium 可以在 hover 之后找到这个新的 div id = "tooltip",它调用了这个 function。

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains    
import time
    
driver_second = webdriver.Firefox(executable_path=DRIVER_PATH)
driver_second.get('https://www.leagueofgraphs.com/summoner/eune/AnFrey99')
time.sleep(3)

infos = driver_second.find_elements_by_class_name('gameDate')

for i in infos:
    hover = ActionChains(driver_second).move_to_element(i)
    hover.perform()
    time.sleep(1)
    DateofGame = driver_second.find_element_by_id('tooltip').text
    print(DateofGame)

output: output:

10.2.2021 14:33:22 - 31min 48s
10.2.2021 14:02:55 - 20min 47s
29.1.2021 04:12:09 - 29min 13s
29.1.2021 03:23:24 - 34min 54s
29.1.2021 02:44:22 - 32min 46s
29.1.2021 01:35:22 - 32min 48s
28.1.2021 23:23:19 - 24min 35s
10.1.2021 01:12:34 - 21min 8s
8.1.2021 22:27:35 - 21min 4s
8.1.2021 22:08:01 - 14min 51s

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

相关问题 Python Selenium 使用 javascript 网站获取工具提示文本的值 - Python Selenium get value of tooltip text with javascript website 如何使用python和JavaScript从另一个网站提取文本? - How can I extract text from another website using python and JavaScript? 如何使用Selenium从JavaScript窗口获取文本? - How to get a text from a JavaScript window using Selenium? 如何使用 Selenium 和 Python 从由空格分隔的文本节点获取文本 - How to get text from textnodes seperated by whitespace using Selenium and Python 使用Selenium Web驱动程序从网站获取隐藏文本值的Javascript - Javascript to get hidden text value from website using Selenium web driver 如何使用Javascript从网站读取文字? - How to read text from a website using Javascript? 如何使用javaScript从网站(页面源)中的href抓取文本? - how can i grab a text from a href in a website (page source) using javaScript? 将硒与python结合使用,如何从JS中声明的HTML中获取Var <script> element - Using selenium with python, how can I get Var from HTML where it's declared in a JS <script> element 如何使用python selenium仅打印任何网站上显示/可见/(显示在屏幕上)的文本内容 - How can I print only displayed/visible/(shown on the screen) text contents of any website using python selenium 如何从 selenium 的输入文本框中获取文本? - How do I get text from input text box in selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM