Search code examples
pythonseleniumselenium-webdriverweb-scrapingtooltip

Selenium - Get text from hidden tooltip by moving mouse horizontally


I need to activate each hidden tooltip in sequence to get the price of average sale for each day as it looks on the chart at the bottom of the page: https://stockx.com/adidas-yeezy-boost-350-v2-israfil ![chart The problem is that tooltip is hidden and the text (price and day) changes every time you move the mouse horizontally. I've tried something by this code, but it still doesn't work and I have no idea what will be working

driver.get('https://stockx.com/adidas-yeezy-boost-350-v2-israfil')
driver.find_element_by_xpath('//*[@id="root"]/div[1]/div[2]/section/div/div[2]/button').click()

time.sleep(3)
driver.find_element_by_xpath('//*[@id="nav-login"]').click()
time.sleep(3)
driver.find_element_by_xpath('//*[@id="email-login"]').send_keys('[email protected]')
driver.find_element_by_xpath('//*[@id="password-login"]').send_keys('abcd')
driver.find_element_by_xpath('//*[@id="btn-login"]').click()

action = ActionChains(driver)
action.move_to_element(driver.find_element_by_xpath('//*[@id="highcharts-hdwe449-0"]/svg'))
element = driver.find_elements_by_xpath('//*[@id="highcharts-w6bxh4h-109"]/svg/path')
driver.execute_script("arguments[0].visibility='visible'", element)
print(driver.find_element_by_class_name('highcharts-label highcharts-tooltip-box highcharts-color-none').text())

I get error:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="highcharts-hdwe449-0"]/svg"}

I will be grateful for help


Solution

  • The reason you are getting NoSuchElementException because there is no element with xpath //*[@id="highcharts-hdwe449-0"]/svg.

    To get the tool tip text you need to first drag your mouse over chart horizontally, then only you will text tag <tspan>. You can use ActionChains class move_to_element_with_offset method as below:

    # Wait for chart to load
    ele = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='highcharts-axis-labels highcharts-xaxis-labels ']")))
    # Scroll to chart header
    driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_xpath("//b[text()='Latest Sales']"))
    #Get all elements for dates below chart
    co_ordinates = driver.find_elements_by_xpath("//*[@class='highcharts-axis-labels highcharts-xaxis-labels ']//*[name()='text']")
    stock_Tracker = {}
    
    # As date intervals on x-axis is for 2 days, We have to use same date element to move our moise twice
    for co in co_ordinates:
        ActionChains(driver).move_to_element_with_offset(co, 0, -35).pause(1).perform()
        try:
           key = driver.find_element_by_xpath(
            "//*[@class='highcharts-label highcharts-tooltip-box highcharts-color-none highcharts-tooltip-header']//*[name()='tspan']").text
           value = driver.find_element_by_xpath("//*[text()='Amount: ']//following-sibling::*").text
           stock_Tracker[key] = 'Amount: ' + value
        except:
            pass
        ActionChains(driver).move_to_element_with_offset(co, 25, -35).pause(1).perform()
        try:
            key = driver.find_element_by_xpath(
                "//*[@class='highcharts-label highcharts-tooltip-box highcharts-color-none highcharts-tooltip-header']//*[name()='tspan']").text
            value = driver.find_element_by_xpath("//*[text()='Amount: ']//following-sibling::*").text
            stock_Tracker[key] = 'Amount: ' + value
        except:
            pass
    
    
    print(stock_Tracker)
    

    Output:

    enter image description here