Not able to get tooltip text using Selenium WebDri

2019-06-07 21:28发布

问题:

I have 5 tooltips in page. Using WebDriver, I am trying to verify these tooltip text.

I am using following code sequentially to get the tooltip text of all 5 elements:

Actions builder = new Actions(WebDriver);
builder.ClickAndHold(Element1).Perform();
Console.WriteLine(Element1ToolTip.text);

builder.ClickAndHold(Element2).Perform();
Console.WriteLine(Element2ToolTip.text);

builder.ClickAndHold(Element3).Perform();
Console.WriteLine(Element3ToolTip.text);

The issue is I get only the tooltip text of first element printed in console. Is it because I need to refresh or reset the builder?

It's really weird when I delete the code for 1st element , then I can get tooltip text of 2nd element. So, basically it is getting tooltip text only once in single execution.

回答1:

Verify tool tip by comparing "title" attribute of the web element and your expected tool tip text.

Console.WriteLine(Element1.GetAttribute("title"));

Console.WriteLine(Element2.GetAttribute("title"));


回答2:

Tool tip text for input elements would be the title attributes and for images, alt attribute would be the tool tip.This is the standard for HTML 4, so I am not sure if you need to do hover and all.

Console.WriteLine(InputElement1.GetAttribute("title"));
Console.WriteLine(ImageElement1.GetAttribute("alt"));

http://www.javascriptkit.com/howto/toolmsg.shtml



回答3:

I think, it needs to release from element as:

builder.release(Element1).perform();

So, your code could be as below:

Actions builder = new Actions(WebDriver);
builder.ClickAndHold(Element1).Perform();
Console.WriteLine(Element1ToolTip.text);
builder.release(Element1).perform();

builder.ClickAndHold(Element2).Perform();
Console.WriteLine(Element2ToolTip.text);
builder.release(Element2).perform();

builder.ClickAndHold(Element3).Perform();
Console.WriteLine(Element3ToolTip.text);
builder.release(Element3).perform();


回答4:

I am facing the same issue , i checked the view source page on running the test and it appears that the title attribute is displayed as data-original-title.Due to which it is unable to display the text.On replacing the title with data-original-title . I am able to obtain text.