Find element by xpath in Katalon Studio


I have an issue. The problem is that I need to select an element with specific color and for that I need to use xpath. Because I am totally new to Katalon Studio, I wanted to try out how the finding of certain elements works with xpath. I tried several ways, but none of them are working. Could someone help me and point me out where is the problem and what I am missing?

One way I tried :
WebDriver driver = DriverFactory.getWebDriver()

WebElement element = driver.findElement(By.xpath("id('extlms-calendar1')/div[@class='calendar-table-view']/div[@class='session-parent-div vertical-align']/div[@class='room-sessions sessions-block fc']/div[@class='scrolling-sessions']/span[17]/a[@class='make-space btn register']"))

Second way I tried :
TestObject testObject = new TestObject()

testObject.addProperty('xpath', ConditionType.EQUALS, "//id('extlms-calendar1')/div[@class='calendar-table-view']/div[@class='session-parent-div vertical-align']/div[@class='room-sessions sessions-block fc']/div[@class='scrolling-sessions']/span[17]/a[@class='make-space btn register']")

None of them are working. If I copy this xpath to Chrome inspect tab where I can type xpath and if it is correct it shows which element it is based on that xpath, it shows that element, but Katalon Studio can’t find it no matter which way of above mentioned I use.

This xpath that I pasted in above mentioned examples is taken from Spy Web and from that certain element that I need. The style that I need to point to is this :
style=“background: rgb(204, 204, 204); color: rgb(255, 255, 255);”

Although I like that you start your xpath at an ID, it seems you are a long way from your final destination. (The longer the distance, the more likely it can break when your page is updated, especially with “span[17]” in the path.)

Can I suggest either to show some HTML that we may assist you more, or use a shorter destination xpath, such as
WebElement element = driver.findElement(By.xpath("//a[@class='make-space btn register']"))

Yep, it is better to write smaller and flexible xpaths. try this one maybe: //a[@class=‘make-space btn register’] if it does not work you can inspect that element and show to us the properties of that element in the HTML.

to learn more about flexible xpaths you can check here :

This is the HTML of the element I need to access through xpath

If you hit CTRL + F while you are in the HTML, and paste the xpath that I submitted above into the Find textbox, does it say “1 of 1” to the right of the textbox or something else?
//a[@class='make-space btn register']

@grylion54 With this //a[@class='make-space btn register'] typed into the Find textbox on Chrome, it says 1 out of 24. In other words, it targets all of the possible buttons. I can minimize this search by making so //a[@class='make-space btn register'][contains(@style, 'background style here')]. With this it says 1 out of 9, but I need only one.

Unfortunately, it depends if you are testing to see if the background colour is the same as what is expected and you are willing to use what may not be true (if a bug exists your usage of “style” and “background” colour would fail), otherwise, I don’t see anything but what you originally had, although I may start closer:

//div[@class="scrolling-sessions"]/span[1]/a[@class="make-space btn register"]

You will have to adjust the number in the span part of the xpath to move through each item, like you had.

You could ask a developer to add an id within one of the buttons to help some with your pathway and therefore, your testing.

This implies that there are 9 <a> elements with the same background color.

OK, then your strategy “Selecting <a> by background-color” is not specific enough. How would you select one out of 9? By index? For example, you can select the 3rd <a> amongst 9 by the following xpath :

(//a[@class='make-space btn register'][contains(@style, 'background style here')])[3]

Or do you want to look up amongst 9 a <a> of the earliest time slot in the afternoon (e.g, 13:00)? — programming this look up process would be possible, but not very easy.