-1

I have to click on an image inside a grid. Grid elements have dynamic ID's. Therefore it's impossible to capture elements as usual. How can click on this image?

I have boxed the image I want to click using a red color. I have tried using classname, css locators. Didn't try xpath because it changes.
Im using testng,java,selenium webdriver, chromdriver

Code of the grid

This is what I have developed upto now using given answers. But I get and exception when I run the code. I have added the code relevant for clicking the image (Which doesn't work as expected).

//Locator class 
public static final String RegistrationArrowButton = "x-tree-elbow-img x-tree-elbow-plus x-tree-expander";  



//Method class      
List<WebElement> listOfElements = driver.findElements(
        By.cssSelector(RecordDealLocators.RegistrationArrowButton)
);                               




//Exception occurd
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at BaseUtils.LaunchDriver.clickList(LaunchDriver.java:47)
at Pages.PurchaseDealPage.PurchasePage.clickNavigateArrow(PurchasePage.java:34)
at Test.DealsTest.PurchaseTest.PurchaseDeal(PurchaseTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Please note that I have used Page Object Model

1
  • Are you able to use the ID's of the TR and/or the TD records? Does every TD record contain the same one DIV and two IMG's? XPath is the way to go, but how to craft it depends entirely on the entire document. – Bill Hileman Aug 15 '18 at 14:57
1

I would try to select all the IMGs that have those three specific classes:

List<WebElement> listOfElements = driver.findElements(
   By.cssSelector("img[class='x-tree-elbow-img x-tree-elbow-plus x-tree-expander']")
);
listOfElements.get(0).click();

The get(0) takes the first found IMG, but you could also use this to click the second if needed.

You can also find elements in other elements. So find the first item of the page that does not change. Now within find the first table, then the first row, the first cell and the first image. This page has some examples: https://seleniumjava.com/2017/12/07/find-child-elements-with-locator-chaining/

WebElement result = driver.findElement(By.id("an_id_that_doesnt_change"));
WebElement table = result.findElement(By.cssSelector("table"));
WebElement first_row = table .findElement(By.cssSelector("tr"));
WebElement first_cell = first_row .findElement(By.cssSelector("td"));
WebElement first_img = first_cell .findElement(By.cssSelector("img"));
first_img.click();
5
  • I have tried what you suggested. I'm getting an exception could you please check it and help me with this? – Joe Aug 16 '18 at 5:31
  • The exception says the listOfElements is empty when it tries to get the first item, I think, but I am not a Java expert. The reason could be that the cssSelector does not return anything, that could be because Selenium is to fast and the content is not drawn yet for example or because the selector is wrong. Maybe you just have to waitFor the element to appear. Maybe you need a deepCss() because the application uses a ShadowDOM. – Niels van Reijmersdal Aug 16 '18 at 9:02
  • First I would learn howto "step by step debug selenium tests in java" or find a Java developer to assist you. Understanding how to read and debugging exceptions is key in understanding the next step. – Niels van Reijmersdal Aug 16 '18 at 9:03
  • 1
    Without the actual application, the full body and all your test-code it is impossible to really help you. I can only guess. – Niels van Reijmersdal Aug 16 '18 at 9:04
  • That's why I didn't even bother trying - except to request additional info in a comment, which went un-answered by the OP. – Bill Hileman Aug 16 '18 at 17:16
0

You have a grid structure so that you can just use coordinates like the image is in first row and first column.

0

Will the src attribute be unique inside the image? You can try something like

Xpath: //img[@src="&lt;Paste that long string here&gt;"]
CSS Selector: img[src='&lt;Paste that long string here&gt;']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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