1

Please help solve this issue. I have chart written using devexpress.

http://image.ibb.co/hRD5oR/chart.png

And I have such test on Selenium. It opens a page with chart and clicks all elements (minimize/maximize, expand/collapse). Here is code:

driver.findElement(By.xpath("/html/body/form/div[3]/div[2]/div[1]/div[1]/div/div[1]")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("/html/body/form/div[3]/div[2]/div[1]/div[1]/div/div[1]")).click();
Thread.sleep(1000);

// Switch to a new window / Get the handle
String parentHandle = driver.getWindowHandle();
System.out.println("Before click Category 1");
// Click bar to open drilldown
//FSMSDashbopadObjects.NCbyReportCategoryBarCategory1(driver).click();
driver.findElement(By.xpath("/html/body/form/div[3]/div[2]/div[1]/div[2]/div/svg/g[8]/g/g/rect[1]")).click();
//*[@id="auditsByBrandDiversey"]/svg/g[8]/g/g/rect[1]
System.out.println("After click Category 1");

The test has failed on this step:

driver.findElement(By.xpath("/html/body/form/div[3]/div[2]/div[1]/div[2]/div/svg/g[8]/g/g/rect[1]")).click();

Selenium can't locate this bar. I suppose that there are some nuances with svg. Please advice how to resolve it.

Manmohan_singh
  • 1,776
  • 3
  • 20
  • 29
  • Your Xpaths are too fragile. Consider narrowing down the tags in your path to the target element. – Manmohan_singh Nov 27 '17 at 10:34
  • 1
    Element is within `` tag and is a possible duplicate of [Selenium WebDriver \[Java\]: How to Click on elements within an SVG using XPath](https://stackoverflow.com/questions/41829000/selenium-webdriver-java-how-to-click-on-elements-within-an-svg-using-xpath) – undetected Selenium Nov 27 '17 at 10:35

1 Answers1

0

I am going to answer your question, but there are multiple solutions for this. Now, if you always have 5 bars, your problem is quite easily solved.

In case of 5 bars, and always 5 bars on your entire page:

This is going to return 5 elements:

driver.FindElement(By.XPath("//g[@class='dxc-markers']/rect");

To be sure, hit F12 in chrome, click on Console, typ: $x("//g[@class='dxc-markers']/rect")

Now if you are getting 5 elements back, use:

driver.FindElement(By.XPath("(//g[@class='dxc-markers']/rect)[1]");
driver.FindElement(By.XPath("(//g[@class='dxc-markers']/rect)[2]");
driver.FindElement(By.XPath("(//g[@class='dxc-markers']/rect)[3]");
driver.FindElement(By.XPath("(//g[@class='dxc-markers']/rect)[4]");
driver.FindElement(By.XPath("(//g[@class='dxc-markers']/rect)[5]");

Now if the amount of bars is variable, you need to create an array of the elements you get back. Once you have the array, instead of the [1] insert the array value there and you are set.

Anand
  • 1,899
  • 1
  • 13
  • 23