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
]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 .
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.
JavaScript
x
14
14
1
driver_second = webdriver.Firefox(executable_path=DRIVER_PATH)
2
driver_second.get("https://www.leagueofgraphs.com/summoner/eune/"+'AnFrey99')
3
time.sleep(5)
4
accept_button=driver_second.find_element_by_xpath("/html/body/div[3]/div/div/div[3]/div[1]/button[2]")
5
accept_button.click()
6
tabel=driver_second.find_element_by_xpath("//table[@class='data_table relative recentGamesTable']")
7
rows=tabel.find_elements_by_xpath("tbody/tr")
8
for row in rows:
9
elements=row.find_elements_by_xpath("td")
10
if(len(elements)>1):
11
script=row.find_element_by_xpath("script")
12
data=elements[2].find_element_by_xpath("a/div[3]")
13
data=script.get_property('innerHTML')
14
Advertisement
Answer
If you hover the div, JavaScript will append a new div to the website with the ID 'tooltip'
(full source of the function here):
JavaScript
1
7
1
var TooltipManager = (function () {
2
[ ]
3
var tooltipElement = $('#tooltip');
4
if (!tooltipElement.length) {
5
$('body').append('<div id="tooltip"></div>'); #New Div appended
6
[ ]
7
Therefore Selenium can find this new div id = “tooltip” after you hover over the div, which calls this function.
JavaScript
1
18
18
1
from selenium import webdriver
2
from webdriver_manager.chrome import ChromeDriverManager
3
from selenium.webdriver.common.action_chains import ActionChains
4
import time
5
6
driver_second = webdriver.Firefox(executable_path=DRIVER_PATH)
7
driver_second.get('https://www.leagueofgraphs.com/summoner/eune/AnFrey99')
8
time.sleep(3)
9
10
infos = driver_second.find_elements_by_class_name('gameDate')
11
12
for i in infos:
13
hover = ActionChains(driver_second).move_to_element(i)
14
hover.perform()
15
time.sleep(1)
16
DateofGame = driver_second.find_element_by_id('tooltip').text
17
print(DateofGame)
18
output:
JavaScript
1
11
11
1
10.2.2021 14:33:22 - 31min 48s
2
10.2.2021 14:02:55 - 20min 47s
3
29.1.2021 04:12:09 - 29min 13s
4
29.1.2021 03:23:24 - 34min 54s
5
29.1.2021 02:44:22 - 32min 46s
6
29.1.2021 01:35:22 - 32min 48s
7
28.1.2021 23:23:19 - 24min 35s
8
10.1.2021 01:12:34 - 21min 8s
9
8.1.2021 22:27:35 - 21min 4s
10
8.1.2021 22:08:01 - 14min 51s
11