首页 > 解决方案 > Tootlip text of svg image is not fetched - Selenium Webdriver

问题描述

<span id="tooltip" class="tooltip">
<svg class="svg-inline--fa fa-question-circle fa-w-16" data-fa-transform="grow-6" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="question-circle" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg="" style="transform-origin: 0.5em 0.5em;">
<g transform="translate(256 256)">
<g transform="translate(0, 0)  scale(1.375, 1.375)  rotate(0 0 0)">
<path fill="currentColor" d="M504 256c0 136.997-111.043 248-248 248S8 392.997 8 256C8 119.083 119.043 8 256 8s248 111.083 248 248zM262.655 90c-54.497 0-89.255 22.957-116.549 63.758-3.536 5.286-2.353 12.415 2.715 16.258l34.699 26.31c5.205 3.947 12.621 3.008 16.665-2.122 17.864-22.658 30.113-35.797 57.303-35.797 20.429 0 45.698 13.148 45.698 32.958 0 14.976-12.363 22.667-32.534 33.976C247.128 238.528 216 254.941 216 296v4c0 6.627 5.373 12 12 12h56c6.627 0 12-5.373 12-12v-1.333c0-28.462 83.186-29.647 83.186-106.667 0-58.002-60.165-102-116.531-102zM256 338c-25.365 0-46 20.635-46 46 0 25.364 20.635 46 46 46s46-20.636 46-46c0-25.365-20.635-46-46-46z" transform="translate(-256 -256)">
</path>
</g>
</g>
</svg>
<!-- <i class="fas fa-question-circle" data-fa-transform="grow-6"></i> -->
<span>Use this button to show/hide the calipers.<br>When the calipers are shown:<br>- Click and hold the caliper lines to drag them.<br>- Click and hold the caliper measurement to drag both calipers together.
</span>
</span>

<span id="tooltip" class="tooltip">
<svg class="svg-inline--fa fa-question-circle fa-w-16" data-fa-transform="grow-6" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="question-circle" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg="" style="transform-origin: 0.5em 0.5em;">
<g transform="translate(256 256)">
<g transform="translate(0, 0)  scale(1.375, 1.375)  rotate(0 0 0)">
<path fill="currentColor" d="M504 256c0 136.997-111.043 248-248 248S8 392.997 8 256C8 119.083 119.043 8 256 8s248 111.083 248 248zM262.655 90c-54.497 0-89.255 22.957-116.549 63.758-3.536 5.286-2.353 12.415 2.715 16.258l34.699 26.31c5.205 3.947 12.621 3.008 16.665-2.122 17.864-22.658 30.113-35.797 57.303-35.797 20.429 0 45.698 13.148 45.698 32.958 0 14.976-12.363 22.667-32.534 33.976C247.128 238.528 216 254.941 216 296v4c0 6.627 5.373 12 12 12h56c6.627 0 12-5.373 12-12v-1.333c0-28.462 83.186-29.647 83.186-106.667 0-58.002-60.165-102-116.531-102zM256 338c-25.365 0-46 20.635-46 46 0 25.364 20.635 46 46 46s46-20.636 46-46c0-25.365-20.635-46-46-46z" transform="translate(-256 -256)">
</path>
</g>
</g>
</svg>
<!-- <i class="fas fa-question-circle" data-fa-transform="grow-6"></i> -->
<span>Use this button to show/hide the calipers.<br>When the calipers are shown:<br>- Click and hold the caliper lines to drag them.<br>- Click and hold the caliper measurement to drag both calipers together.
</span>
</span>

** Please find attached code snippet.

Unable to retrieve tooltip message when moving mouse to element.

what is happening , at few instance tooltip is displayed but it is getting disappeared within fraction of seconds so text is not getting retrieved.

Various options tried out :

1.

Actions action = new Actions(driver);<br/>
action.MoveToElement(ElementHandler.FindElement(By.XPath("(//*[name()='svg' and @class='svg-inline--fa fa-question-circle fa-w-16']")));<br/>
Thread.Sleep(2000);<br/>
action.Perform();<br/>
action.MoveByOffset(1, 1).Build().Perform();
  1. Actions action = new Actions(driver);<br/>
      action.MoveToElement(ElementHandler.FindElement(By.XPath("//span[@id='tooltip']")).Perform();<br/>
      string theTextIWant = (string)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].value;", driver.FindElement(By.Xpath("//span[@id='tooltip']//span")));
    

** Span tag text of tooltip is fetched only on hovering. None of the options are working.
The main challenge is tooltip is disappearing within seconds, hence unable to get text. Other than ClickAndHold method if any other solution is provided will be really helpful.
Will be really gratefully if any solution/approach is provided.

标签: javascripthtmlseleniumselenium-webdriverselenium-chromedriver

解决方案


您可以尝试尽快从工具提示中获取文本

Actions action = new Actions(driver);        
action.MoveToElement(ElementHandler.FindElement(By.XPath("//span[@id='tooltip']")).Perform();
            
WebElement toolTip = driver.findElement(By.xpath("//*[@id="tooltip"]"));
String toolTipText = toolTip.getText();
System.out.println("toolTipText-->"+toolTipText);

推荐阅读