dr4g1116
dr4g1116

Reputation: 258

Selenium can't find element by xpath

I've been working at this for some time now. I'm using Selenium and WebDriver version 2.33 (with all browsers). I'm using Java, which should be arbitrary. What I'm doing is simply find an element and hover over it, which I have done in earlier code. But for some reason, I can't get this one to work. I'm trying to get an element with this xpath, obtained by right-clicking the element in the HTML in Chrome and clicking "copy xpath":

//*[@id="highcharts-10"]/svg/g[7]/g/rect[1]

This is how I'm trying to get the element (due to "highcharts-10" dynamically changing):

//*[starts-with(@id, 'highcharts')]/svg/g[7]/g/rect[" + barOption + "]

barOption is inputting correctly (there are a bunch of bars that I'm trying to go through)

Here is my Java code:

WebDriverWait wait = new WebDriverWait(getWebDriver(), 5);
WebElement element;
WebDriver driver = getWebDriver();
By by = By.xpath("//*[starts-with(@id, 'highcharts')]/svg/g[7]/g/rect[" + barOption + "]");
Actions action = new Actions(driver);
WebElement elem = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
action.moveToElement(elem);
action.perform();

What am I doing incorrect here? I've tried using switchTo() statements, but there are no iframes that I can correctly switch to. Here is a picture the HTML because I can't get my hands on the actual text:

UPDATED HTML LINK: http://i1250.photobucket.com/albums/hh527/dr4g1116/Capture_zps6e2bc1b9.png

Anyone have any suggestions for me? Please let me know what I'm doing wrong!

Thanks!!

Upvotes: 3

Views: 7296

Answers (4)

dr4g1116
dr4g1116

Reputation: 258

I just wanted to give a slight update on this. It seems selenium can't see past the SVG tags, so with that being said, I need to find a method to see around them...I will report back if I'm able to find out how.

Thanks all!

Upvotes: 1

Amol
Amol

Reputation: 1

Sorry just seeing your comment on my earlier answer. You can get the values for each bar in barchart by following way:

var barValues = new List<string>(); 
var actions = new Actions(webDriver); //webDriver is instance of selenium WebDriver.
var chartSeriesGroup = webDriver.FindElement(By.ClassName("highcharts-series-group"));
var chartSeries = chartSeriesGroup.FindElement(By.ClassName("highcharts-series"));
var rectTags = chartSeries.FindElements(By.TagName("rect")); //To get all bars in barchart.

foreach (var rect in rectTags)
{
   actions.MoveToElement(rect).Perform(); //Hover mouse on bar.
   var trendMarkers = webDriver.FindElement(By.ClassName("highcharts-tooltip"));
   barValues.Add(trendMarkers.Text); //Storing tooltip value of bar for later use.
}

I am using same method in my current project for getting values of bars in bar chart. Hope this will help you.

Note : If tooltip for bar shows other information e.g.name etc along with value then you need to write logic for extracting the value part from the complete information stored in barValues.

Upvotes: 0

Amol
Amol

Reputation: 1

From your discussion with Amey i deduced that you have only one highchart. so try directly searching for element "highcharts-tracker" using classname i.e By.ClassName("highcharts-tracker") and then hover on this element itself. This would exactly do what you want to achieve.

Upvotes: 0

Arup Rakshit
Arup Rakshit

Reputation: 118299

Try as CSS Selectors :

By by = By.css('div[id^="highcharts"] g[class^="highcharts"] > g > rec')

g.class_name I used,as that <g> tags class name is not visible. Replace that class name with the proper class name.

Upvotes: 2

Related Questions