简体   繁体   中英

How to get Text from Tooltip using Selenium in Java?

I'm trying to get the Text of a tooltip in Java

The curious thing is that I get any attribute of the span, even the TagName() , but when using getText() get an empty String

HTML code :

<div id="currentRebalancing1">
    <div class="chart" id="donutCR1" data-highcharts-chart="8">
        <div id="highcharts-cva4evt-30"
             style="position: relative; overflow: hidden; width: 400px; height: 570px; text-align: left; line-height: normal; z-index: 0; left: 0.0999756px; top: 0.683334px;"
             class="highcharts-container ">
            <svg version="1.1" class="highcharts-root " width="400" height="570" viewBox="0 0 400 570">
                <g class="highcharts-series-group">
                    <g class="highcharts-series highcharts-series-0 highcharts-pie-series highcharts-color-undefined highcharts-tracker "
                       transform="translate(10,47) scale(1 1)">
                        <path fill="RGB(19,37,91)"
                              d="M 206.97372615672333 91.00000267563894 A 129 129 0 1 1 79.03227795957477 236.2868694286062 L 130.21936677574485 229.77212165716372 A 77.4 77.4 0 1 0 206.984235694034 142.60000160538334 Z"
                              transform="translate(0,0)" stroke="#ffffff" stroke-width="1" stroke-linejoin="round"
                              class="highcharts-point highcharts-color-0 "></path>
                        <path fill="RGB(226,0,121)"
                              d="M 79.01605507671634 236.1588935844597 A 129 129 0 0 1 97.18697447921456 152.30879358460714 L 141.11218468752872 179.38527615076427 A 77.4 77.4 0 0 0 130.2096330460298 229.69533615067581 Z"
                              transform="translate(0,0)" stroke="#ffffff" stroke-width="1" stroke-linejoin="round"
                              class="highcharts-point highcharts-color-1 "></path>
                        <path fill="RGB(230,45,140)"
                              d="M 97.25472058085626 152.1990144229889 A 129 129 0 0 1 206.82082158798877 91.00012443766983 L 206.89249295279325 142.60007466260188 A 77.4 77.4 0 0 0 141.15283234851375 179.31940865379335 Z"
                              transform="translate(0,0)" stroke="#ffffff" stroke-width="1" stroke-linejoin="round"
                              class="highcharts-point highcharts-color-2 "></path>
                    </g>
                    <g class="highcharts-markers highcharts-series-0 highcharts-pie-series highcharts-color-undefined "
                       transform="translate(10,47) scale(1 1)"></g>
            </svg>
            <div class="highcharts-label highcharts-tooltip"
                 style="position: absolute; left: 160px; top: -9999px; opacity: 0; pointer-events: none; visibility: visible;">
                <span style="font-family: &quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Arial, Helvetica, sans-serif; font-size: 12px; position: absolute; white-space: nowrap; color: rgb(51, 51, 51); margin-left: 0px; margin-top: 0px; left: 8px; top: 8px;">
                Carmignac Sécurité A EUR Acc  
                    <b>73.00%</b>
                </span>
            </div>
        </div>
    </div>
</div>

Java code:

s_Path = "//*[@id='currentRebalancing1']//*[contains(@class,'highcharts-point highcharts-color-0')]";

Actions builder = new Actions(driver);
WebElement w_AssetWeight = driver.findElement(By.xpath(s_Path));
                builder.moveToElement(w_AssetWeight).build().perform();
Thread.sleep(2000);

s_Path = "//*[@id='currentRebalancing1']//div[contains(@class, 'highcharts-label')]//*[contains(. , 'Carmignac')]";

String s_Result = driver.findElement(By.xpath(s_Path)).getAttribute("style");
// Here s_Result has the value of 'Style'
s_Result = driver.findElement(By.xpath(s_Path)).getTagName();
// Here s_Result = "span"
s_Result = driver.findElement(By.xpath(s_Path)).getText();
//Here s_Result = "" instead of "Carmignac Sécurité A EUR Acc 73.00%"

First value of s_Result is as expected.

Second value of s_Result is "span" (as expected).

Third value of s_Result is "" ("Carmignac Sécurité A EUR Acc 73.00%" was expected)

The desired element is a dynamic element so to extract the tooltip you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies :

  • cssSelector :

     System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#currentRebalancing1 div.highcharts-label.highcharts-tooltip"))).getText()); 
  • xpath :

     System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='currentRebalancing1']//div[@class='highcharts-label highcharts-tooltip']"))).getText()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM