4

I met a problem when I try to get an attribute of one tag.

Here is the html code:

<rect x="66.5" y="43.5" width="26" height="13" fill="#86E067" stroke="#ffffff" stroke-width="1" class="highcharts-point "></rect>

Here is the xpath for that html:

//*[@id="highcharts-z14trt3-4"]/svg/g[6]/g[1]/rect[2]. 

I copied this xpath from Chrome directly,

I am trying to get the attribute height, and here is what I did:

WebElement test = driver.findElement(By.xpath("//*[starts-with(@id,'highcharts')]/svg/g[6]/g[1]/rect[2]"));

I cannot use tagName as there are many tags called rect. Meanwhile, I cannot use the exactly same XPath as the id is dynamic.

So the result is that selenium says cannot locate the element.

Is the way I try to locate this element wrong? Any suggestions?

Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
asked Jun 8, 2017 at 6:16
1
  • 1
    If possible also post your exception text and the full HTML or a URL. Commented Jun 8, 2017 at 9:10

2 Answers 2

4

svg has a default namespace http://www.w3.org/2000/svg. You need to handle namespaces. Either with a prefix:

//*[starts-with(@id,'highcharts')]/svg:svg/svg:g[6]/svg:g[1]/svg:rect[2]

Or, with local-name():

//*[starts-with(@id,'highcharts')]/*[local-name() = 'svg']/*[local-name() = 'g'][6]/*[local-name() = 'g'][1]/*[local-name() = 'rect'][2]
answered Jun 8, 2017 at 11:52
0
3

Solution 1: Your XPath is not valid. You are missing a ' after the word highcharts.

Solution 2: You are using findElement. Can you try using an explicit wait for the element to become visible? If your implicit waiting time is 0, findElement immediately throws an exception (it doesn't retry) which is understandable if your rect isn't loaded already when your page is ready.

answered Jun 8, 2017 at 6:26
1
  • I was trying $x('*//svg:svg') on the website demo.bpmn.io but can't get it to work. I opened a question for this on sqa.stackexchange.com/questions/34437/… Commented Jun 25, 2018 at 16:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.