I can't seem to select the correct element to trigger Selenium's hover actions. I'm able to use cookies to login, and can see the page load, then scroll down, but I must be selecting the incorrect xpath element, and cannot complete the hover to grab data from the desired table. W/o a login, you won't be able to see the table I see, so I'll place some of the HTML below, along with the error message I get, plus a screenshot:
from selenium import webdriver
from selenium.webdriver.firefox import firefox_profile
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import numpy as np
import pandas as pd
import re
url = 'https://www.oddsportal.com/tennis/czech-republic/wta-ostrava/dodin-oceane-linette-magda-h4MPchI4/'
fp = webdriver.FirefoxProfile('/Users/Frontwing/Library/Application Support/Firefox/Profiles/zku6ulmv.ProxyTest')
driver = webdriver.Firefox(executable_path="config/geckodriver",firefox_profile=fp)
driver.get(url)
items = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[7]/div[3]") \
.get_attribute("innerHTML")
def get_cash_data():
# I. Get the raw data by hovering and collecting
xpath = "/html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[7]/div[3]"
data = driver.find_element_by_xpath(xpath)
driver.execute_script("arguments[0].scrollIntoView();", data)
hov = ActionChains(driver).move_to_element(data)
hov.perform()
data_in_the_bubble = driver.find_element_by_xpath(xpath)
hover_data = data_in_the_bubble.get_attribute("innerHTML")
# II. Extract opening odds
b = re.split('<br>', hover_data)
#c = re.split('\(([^()]\d+)\)', b)
#opening_odd = c
#print(opening_odd)
print(hover_data)
return(b)
#print([x.text for x in items])
get_cash_data()
Here is the HTML snippet of the target I want to hover over:
<td class="right odds up"><div onmouseout="delayHideTip()" onmouseover="page.hist(this,'E-0.00-0-0','55fmkx2s5a4x0xdlo2u',44,event,0,1)" style="display: block;">2.42<br>(42)</div></td>
I figured xpath might be an easier way to pinpoint the element, but perhaps not? Here's the error message:
File "opstest.py", line 44, in get_cash_data() selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .right odds
Particularly, I suspect the issue has to do with the xpath and likely also the hov workflow. I must not be doing it correctly, or in the correct order..
