How to get list of elements on a page and than click on the first element

Hi Everyone,
Today I am automating a test case. I have page where I can see the different cards and each card has “three dots” which are showing some options.
I am collecting the list of all “three dots” So I can click on any three dot from the list. Please help me.
In the following code, I want to click on the first “three dots” element.

Here is the code:

WebUI.navigateToUrl(GlobalVariable.EnvURL)
List elements = WebUI.findWebElements(findTestObject('Campaign management/three dots'), 30)

WebUI.click(elements.get(0), FailureHandling.STOP_ON_FAILURE)

When I run this code, it is giving me the error.

You are trying to combine a WebElement with a TestObject. Instead, try the below:

WebUI.navigateToUrl(GlobalVariable.EnvURL)
WebUI.waitForPageLoad(10)

List<WebElement> elements = WebUI.findWebElements(findTestObject('Campaign management/three dots'), 30)

elements.get(0).click();   // here use the Web Element from the list

And if you have any further concerns, show us the error you are getting to give us more information.

you can try also the groovy features, sort of just elements =

groovy “should be” smart enough to cast the propper class / type for your object.

anyway, since you are most likely sticking with strong typing, as you do the rest of your code, my advice is to enhance your java skills and be aware of what this means.
casting with a wrong object type it is a common mistake i saw, and not only from begginers in java.
choose your path :slight_smile:

for the rest, the sentence ‘i got error’ tell us nothing.
until we see the actual error you get, we will say ‘the error is between your chair and your keyboard’

1 Like

@grylion54 pretty much answered this question already (you should probably accept that answer right now), but I’ma add to it…

TestObjects and WebElements have nothing to do with each other on the inheritance tree. You cannot use one for the other.

If you absolutely need to use WebUI methods, and other methods exclusive to TestObjects (for example, scrolling all the Test Objects into view , or verifying that they are present), I advise you use

WebUI.convertWebElementToTestObject(yourWebElement);

Thank you. It worked for me. I was trying this before but looks like I was not importing the correct ones.