如何使用Java在工具提示中找到span元素

时间:2017-05-18 10:21:31

标签: selenium

我在span标记中定位元素时遇到问题。

当我将鼠标放在上面时,它会显示工具提示的一部分。

我将在下图中向您展示:https://i.stack.imgur.com/b1qTg.jpg data from tooltip

当我将鼠标放在该点上时,会出现工具提示。我需要获取该数据以验证Selenium Webdriver中的网页文本。

我已尝试使用xpath编写此代码,但它不会返回任何数据:

//dot of the highchart where I put my mouse to see the tooltip. Selenium Webdriver doesn't find it and it causes an error, it stops the execution here

WebElement element = driver.findElement(By.xpath(".//*[@id='highcharts-4']/svg/g[5]/g[2]/path[5]"));  //dot's xpath

// Use action class to mouse hover on the dot
Actions action = new Actions(driver);
action.moveToElement(element).build().perform();
WebElement toolTipElement = driver.findElement(By.xpath(".//*[@id='highcharts-4']/div[1]/span"));      //xpath of the shown tooltip

// To get the tool tip text 
String toolTipText = toolTipElement.getText();

任何可以获取内部数据的想法?非常感谢你的帮助!!!!!

2 个答案:

答案 0 :(得分:0)

解决了点击点!!!

//我的代码 WebElement element = driver.findElement(By.xpath(" .// [@ id =' highcharts-4'] / [name()=' SVG'] / [名称()=' G&#39] [5] / [名称()=' G'] [2] / * [名称()='路径'] [1]&#34));

    // Use action class to mouse hover on Text box field
    Actions action = new Actions(driver);
    action.click(element).build().perform();

答案 1 :(得分:0)

使用以下内容:

\\first click the dot using your code:
Actions action = new Actions(driver);
action.click(element).build().perform();

\\then use following code to get your data :

System.out.println("First line of Tooltip" + driver.findElement(By.cssSelector("div > div.highcharts-tooltip > span span:nth-child(1) > b")));

System.out.println("Second line of Tooltip" + driver.findElement(By.cssSelector("div > div.highcharts-tooltip > span span:nth-child(2) > b")));