Selenium - 通过水平移动鼠标从隐藏的工具提示中获取文本

Selenium - Get text from hidden tooltip by moving mouse horizontally

我需要按顺序激活每个隐藏的工具提示,以获取页面底部图表上显示的每一天的平均销售价格:https://stockx.com/adidas-yeezy-boost-350-v2-israfil 问题是工具提示被隐藏,每次水平移动鼠标时文本(价格和日期)都会改变。我已经通过这段代码尝试了一些东西,但它仍然不起作用,我不知道什么会起作用

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('abcd@gmail.com')
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())

我收到错误:

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

我将不胜感激

你得到 NoSuchElementException 的原因是 xpath //*[@id="highcharts-hdwe449-0"]/svg 没有元素。

要获得工具提示文本,您需要先将鼠标水平拖到图表上,然后只有文本标记 <tspan>。您可以使用 ActionChains class move_to_element_with_offset 方法如下:

# 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)

输出: