Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SeleniumWebDriver在鼠标上方获取工具提示文本_Selenium_Selenium Webdriver_Tooltip - Fatal编程技术网

如何使用SeleniumWebDriver在鼠标上方获取工具提示文本

如何使用SeleniumWebDriver在鼠标上方获取工具提示文本,selenium,selenium-webdriver,tooltip,Selenium,Selenium Webdriver,Tooltip,鼠标悬停在图标工具提示上后,我无法获取工具提示文本。我需要得到工具提示文本,这是html代码 <a class="tooltip" onclick="SocialAuth.showOpenIDLoginWindow('google');_gaq.push(['_trackEvent','LoginForm','google-login','google login was clicked']);" href="javascript:void

鼠标悬停在图标工具提示上后,我无法获取工具提示文本。我需要得到工具提示文本,这是html代码

<a class="tooltip" onclick="SocialAuth.showOpenIDLoginWindow('google');_gaq.push(['_trackEvent','LoginForm','google-login','google login was clicked']);" href="javascript:void(0);"><span>We dont store your password or access your contacts on your Google account.</span><img class="social" height="39" width="40" src="/images/login/google.png"/>
我们不会在您的谷歌帐户上存储您的密码或访问您的联系人。

使用下面的代码行从元素中获取工具提示文本

String toolTipText = driver.findElement(By.id(element's id)).getAttribute("title");

您必须为此使用操作。在这里,我在谷歌上打印鼠标悬停信息

    Actions ToolTip1 = new Actions(driver);
    WebElement googleLogo = driver.findElement(By.xpath("//div[@id='hplogo']"));

    Thread.sleep(2000);

    ToolTip1.clickAndHold(googleLogo).perform();
使用“clickAndHold”方法执行鼠标悬停操作

使用“getAttribute”命令获取工具提示的值

    String ToolTipText = googleLogo.getAttribute("title");

    Assert.assertEquals(ToolTipText, "Google");

    Thread.sleep(2000);
    System.out.println("Tooltip value is: " + ToolTipText);

从工具提示获取文本的方法与使用Jquery工具提示时的HTML方法不同。 getAttribute()在使用Jquery工具提示时不起作用。如果您在中看到工具提示示例,则它是jquery工具提示

下面的代码在这里工作:

    WebDriver driver=new FirefoxDriver();
    driver.get("http://demoqa.com/tooltip/");
    WebElement element = driver.findElement(By.xpath(".//*[@id='age']"));
    Actions toolAct = new Actions(driver);
    toolAct.moveToElement(element).build().perform();
    WebElement toolTipElement = driver.findElement(By.cssSelector(".ui-tooltip"));
    String toolTipText = toolTipElement.getText();
    System.out.println(toolTipText);
一个好的参考是:


不久前我回答了一个类似的问题,您可以在此处查看:。我相信,您可以在selenium中执行类似的操作。webdriver会返回什么错误?请尝试在整个源代码中查找工具提示。它可能在其他地方。请发送您要废弃的网站。请使用以下代码重试<代码>驱动程序.findElement(按.class(工具提示)).getAttribute(“span”)实际上我有3个图标具有相同的类名和cssSelector,这就是为什么它不是鼠标悬停在图标本身上的原因。然后,尝试使用xpath中的position()帮助。请尝试以下代码
By.xpath(“//a[@class='tooltip']和[position()=2]”)