2

enter image description here

So I am trying to automate a test and my issue is that I cannot point to the button element which is actually an image inside of a div. I tried going down the hierarchy but no luck. this is the code I used.

driver.FindElement(By.XPath("./tr/td[@id='__HorizontalSplitter2L']/div[@id='__Ho
rizontalSplitter2LD']/div[@id='HorizontalSplitter2_LeftP_Content']/div[@id='R2Container']/div[@id='R2ToolStripContainer']/div[@class='ToolStripVertical 
ToolStrip']/div[@id='861f18ee-dce6-40e9-bac1-927b4768f72b']/img")).Click();
3
  • 1
    Try this: driver.FindElement(//div[@id='R2ToolStripContainer']/div/div/img).Click(); - This basically says give me first div, from root, that Has id R2ToolStripContainer, then first child div of that, first child div on that, then img under that. The id on your last div scares me, in that it has feel like it could not always be the same, so I start with the first ancestor element that as a "trustworthy" id. Using // to start things out so initially all descendants from root are considered.
    – evets17
    Mar 14, 2018 at 13:08
  • 1
    @evets17 - don't write answers in comments. If your answer is good, you will get karma points, but none for comments. Mar 14, 2018 at 13:59
  • Ditto what @evets17 said- please post it as an answer, and original poster, please select it as accepted so that others seeking similar questions can see that it's a valid answer, and so that others looking to answer questions can see that it's already answered. Mar 14, 2018 at 20:23

2 Answers 2

1

I found a smarter way. google chrome offers a variety of tools to get the xpath. I used them , it saves time and is accurate, overall efficiency in automating test cases.

0

Try this:

driver.FindElement(//div[@id='R2ToolStripContainer']/div/div/img).Click(); 

This basically says give me first div, from root, that Has id R2ToolStripContainer, then first child div of that, first child div on that, then img under that.

The id on your last div scares me, in that it has feel like it could not always be the same, so I start with the first ancestor element that has a "trustworthy" id.

Using // to start things out so initially all descendants from root are considered.

3
  • Hi, I tried it out but it did not work ........... driver.FindElement(By.XPath("//div[@id='R2ToolStripContainer']/div/div/img")).Click(); NO SUCH ELEMENT EXCEPTION.
    – Evan
    Mar 15, 2018 at 4:49
  • Also , you should know that the click event in the backed is triggered when the last div is clicked ,so I took the img off the path and tried but still it did not work, any idea , perhaps absolute path ?
    – Evan
    Mar 15, 2018 at 4:56
  • I guess possible that 'R2ToolStripContainer' is not a unique id, and therefore finding another div with that ID? Another approach could be: driver.FindElement(By.XPath(//div[@title='Add a new relation aa']).Click(); A trick you could do for debugging is var test = driver.FindElements(By.XPath("//div[@id='R2ToolStripContainer']); and see how many if any it returns. Note the s on FindElements, as that returns all elements that match criteria.
    – evets17
    Mar 16, 2018 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.