Home > OS >  Python Selenium no such element: Unable to locate element using xpath after move to element action
Python Selenium no such element: Unable to locate element using xpath after move to element action

Time:04-07

I am not able to locate a div that seems to be always present containing tooltip text after performing a hover action.

I'm new to web scraping and try to get some odds data from a betting website using selenium. I would like to extract the tooltip data which appears for each of the cells.

I tried the following code and I time out. If I just look for the tooltiptext element with find_element then I get an element not found error. The issue I have is that I can see this element in chrome's inspector but seem to be unable to locate it within the page.

#set up webdriver
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# initialize the Chrome driver
wd = webdriver.Chrome('chromedriver',options=chrome_options)
#get the website data
wd.get("https://www.oddsportal.com/soccer/argentina/primera-nacional/quilmes-san- 
martin-t-n791ygOf/")
wd.maximize_window()

#Hover over element and get tooltip text

element = wd.find_element(by = By.XPATH, value='//*[@id="odds-data- 
table"]/div[1]/table/tbody/tr[1]/td[2]')
hover = ActionChains(wd).move_to_element(element)
hover.perform()
tooltipText = WebDriverWait(wd, 10).until(EC.presence_of_element_located((By.XPATH,
'//*[@id="tooltiptext"]'))).text    

see image below of the tooltip div:

enter image description here

CodePudding user response:

  1. You will have to click on Accept cookie.

  2. Use innerText instead of text with explicit wait.

Code:

driver.maximize_window()
wait = WebDriverWait(driver, 20)

driver.get("https://www.oddsportal.com/soccer/argentina/primera-nacional/quilmes-san-martin-t-n791ygOf/")

#Hover over element and get tooltip text

try:
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
except:
    pass


element = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='odds-data-table']/div[1]/table/tbody/tr[1]/td[2]")))
ActionChains(driver).move_to_element(element).perform()

tooltipText = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div#tooltipdiv"))).get_attribute('innerText')
print(tooltipText)

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

Output:

04 Apr, 23:52 2.68  0.02
04 Apr, 23:44 2.66  0.05
04 Apr, 22:37 2.61  0.03
04 Apr, 22:28 2.58  0.04
04 Apr, 21:44 2.54  0.02
04 Apr, 20:00 2.52 -0.16
04 Apr, 19:51 2.68  0.15
04 Apr, 18:40 2.53  0.01
04 Apr, 17:31 2.52 -0.01
04 Apr, 13:56 2.53 -0.01
04 Apr, 12:16 2.54 -0.06
04 Apr, 12:15 2.60  0.14
04 Apr, 12:03 2.46 -0.02
04 Apr, 11:28 2.48  0.04
04 Apr, 05:26 2.44 -0.16
04 Apr, 00:32 2.60  0.01
03 Apr, 16:36 2.59  0.02

Opening odds:
29 Mar, 13:51 2.57
  • Related