How to select and click an element without the ID


#1

Hi,

I’m testing my page with a list of member (like 200 divided in several pages).
I would like to click every pages: the problem is that I don’t want to create 200 objects…how can I just tell Katalon to click the first element, than the second one, and so on?

Thank you,
Stefano


#2

If there’s a button that says “Next Member” just write a test that clicks that button over and over.

Oh, there’s no such button?

Tell you what, to save me (and the rest of us) from guessing, and to help you help us, read the following link and post back with any relevant information (and PLEASE read it… it contains all the info you need to help us and you!)


#3

Hi
You can use dynamic objects. So in a loop that ranges from 1 to 200 you can write a click on that object where the range index is passed as a parameter.
object’s xpath must be given in the attributes section
div/div[1]-- is the first object
div/div[{RowNumber}] – dynamic object
WebUI.click(findTestObject(‘Object’,[(‘RowNumber’) : index]))
Regards
Kavya


#4

Ok, you are right.

No, unfortunately there is not a “next member” button.

The link is:

The project, right now is simple because the first step for me is achieving clicking every link.

So, I create the action of click (Object repository) and I put the css (“Css selection”) that I found with the plugin “ChroPath”:
body.archive.search.search-results.tax-company_category.term-bioprinting.term-548:nth-child(2) section.section.content:nth-child(2) div.content-result-def:nth-child(4) div.content-wrapper div.zone-content.equalize.zone.clearfix div.content-container.container-16 div.companies-listings.block div.company-search-page-conatiner:nth-child(7) div.company-listing.clearfix:nth-child(2) div.listing-body div.listing-title > a:nth-child(1)

And I saw that the first link has “…nth-child(1)…”, and the second one has “…nth-child(2)…”, but it’s not working.

Obviously I can’t use the “relXPath”, because gives me the ID of the link (that’s always changing).

The error is:

tack trace: com.kms.katalon.core.exception.StepFailedException: Unable to click on object ‘Object Repository/3D/Page_You searched for 3D Printing/Page_You searched for 3D Printing/a_Read More’ (Root cause: org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (228, 278). Other element would receive the click:


(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

Thank you


#5

Thank you.

So in the attributes section, what I put in “name”, “Condition” and “Value”?

Thanks!


#6

This error is usually caused by something else being in the way. Try waiting for your element to be visible before accessing it.


#7

Hi
Just inspect the object in the chrome developer tools. Copy xpath of the object one by one and see where the index is getting changed and frame the xpath like what I wrote in the above answer. Then click on add attribute in object properties and copy it in the xpath equals the copied attribute path.
Please share the xpaths of 2 or 3 objects so that I can give you a better solution.

Regards
Kavya


#8

Hi
May be you can try this.

Regards
Kavya


#9

I was waiting, but nothing. The links are visible.


#10

But in this script they control if the link are accessible. How can I, for example, open every link?


#11

I already did it. And I find that the index that is getting changed is:

/html[1]/body[1]/section[1]/div[4]/div[1]/div[1]/div[1]/div[1]/div[6]/div[2]/div[1]/a[1]

So I put it:

immagine

But it doesn’t work.

If I record the click of the link, Katalon create this action:

immagine

Thank you


#12

Hi,
You have to set the xpath in the attributes.


This looks like the below

In the script use the object like this
WebUI.click(findTestObject(‘Object’,[(‘RowNumber’) : indexinthefoorloop]))
Regards
Kavya


#13

doesn’t work…it doesn’t click. I tried with a number instead of “$(LinkNumber)” but nothing. Did you try your script with web page?


#14

Hi
I will try on your web page and let you know. The syntax is ${LinkNumber} :smile:
By the way is you page 3D printing business directory?


#15

Hi
The xpath in the webpage is not constant. So we cannot substitute an index with a variable to access all the links. This generally works with the table data where everything is constant except the row number or some layout that is similar to this. So I have posted a question here.


If we get a solution on this we can achieve your project requirement. When is the deadline?

Regards
Kavya


#16

Hi Kavya

thank you for your engagement!
Ok, so I have to find a new solution…I don’t have any deadline.

Thank you!
Stefano


#17

Hi Stefano
We’ll find a solution soon.

Regards
kavya


#18

With all the engagement on this topic, I figured it had already been worked out :sweat_smile: Lets take this step by step, in detail, and I will give an example solution at the end for what I think you are trying to accomplish:

1.) Locating the links

On the page you linked, I’m assuming that you want to click on the Read More “button” (really, a link) for each search result:

image

Note: You could also click the link associated with the “listing title” to achieve the same goal:

image

So, lets create a Test Object with an XPath that will identify all links of this type. I will write an Xpath for the Read More button, but you could easily do this for the other link:

2.) Working with the pagination

You mentioned that you want to click ALL links, and that the set of all links is divided into pages (i.e. they are “paginated”):

image

This means that you will need another Test Object to manipulate the pagination widget as well. This can be handled in a couple of ways, but I’ll just follow the same idea we’re using for the links themselves and grab all page links that are available:

3.) Test code

Since you haven’t specified what you’re actually trying to do beyond “click every link”, I will write the test code based on the assumption that that’s literally all you are trying to do, and you can fill in the rest:

import org.openqa.selenium.WebElement

import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.webui.common.WebUiCommonHelper
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser("https://www.3dprintingbusiness.directory/?s&where&company_category=bioprinting&cat=bioprinting&search_simple=Search#");

// Get a list of all pages in the pagination widget:
List<WebElement> pageLinks = WebUiCommonHelper.findWebElements(ObjectRepository.findTestObject("pageLink"), 30);

// Loop through each page in the pagination:
for(WebElement pageLink : pageLinks) {
	
	// Click the page link:
	pageLink.click();
	
	// Get a list of links for the current page:
	List<WebElement> searchResultLinks = WebUiCommonHelper.findWebElements(ObjectRepository.findTestObject("searchResultLink"), 30);
	
	// Loop through each result link in the page:
	for(WebElement searchResultLink : searchResultLinks) {
		
		// Click the result link:
		searchResultLink.click();
		
		// Do some validation? You haven't further specified what you're trying to do with each link...
		
		// Return to the search results page:
		WebUI.back();
	}
}

4.) Final notes

The idea here is to get a List of WebElements that you can do whatever you want with. This is much easier (in my opinion) than trying to get individual WebElements using some dynamic XPath, as has been suggested. With this approach, you simply get a list of all target elements, then you can either loop through them, as I’ve shown above, or you can select individual elements from the list using an index:

List<WebElement> searchResultLinks = WebUiCommonHelper.findWebElements(ObjectRepository.findTestObject("searchResultLink"), 30);
WebElement firstLink = searchResultLinks.get(0);
WebElement secondLink = searchResultLinks.get(1);
.
.
.

Hopefully this helps! :metal:


#19

Thank you Brandon for your code!!

I add the two variables:

immagine
that’s true?

Unfortunately, I got this error:
" unknown error: Element is not clickable at point (284, 16). Other element would receive the click:

"

The error is at Statement - pageLink.click()

thank you!


#20

Hi
This solution is good. I was just looking for a solution where creation of one object is enough as per the requirement of charliar.


There was a solution given for this. We have to try it out.
Anyways thanks for your solution. It helps.

Regards
kavya