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/a[@class='make-space btn register']"))
Second way I tried :
TestObject testObject = new TestObject()
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” 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.
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:
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 :