How can I find and click elements in my calendar?

In my TC I need to double click on a certain day of a calendar and then coordinate a certain hour (see picture)


In this case, I cannot select a day before the actual day.
I am having a lot of trouble trying to double click in one of the days of the agenda.

This is what I was trying to do:

WebDriver driver = DriverFactory.getWebDriver()

WebElement Table = driver.findElement(By.className('k-scheduler-content'))

List<WebElement> Rows = Table.findElements(By.tagName('tr'))

println('Rows: ' + Rows.size())

println('Rows: ' + Rows.get(0).getText())

//List<WebElement> tr_withImg = Table.findElements(By.xpath('//tr[*//img[contains(@id, \'vMODIFICAR_\')]]'))
Date today = new Date()

String todaysDate = today.format('mm/dd/yy').substring(3, 5)

for (int i = 0; i < Rows.size(); i++) {
    List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))

    for (int j = 0; j < Cols.size(); j++) {
        if (Cols.get(j).getText().equals(todaysDate)) {
            println(Cols.get(j).getText())
			print(i + " " + j + " ")
			
			TestObject toV = new TestObject()
			String objectXpathV = CustomKeywords.'keywordPrueba.CustomFunction.createIdRelative'('////div[@id=\'schedulerSch\']/table/tbody/tr[2]/td/div/table/tbody/tr[', i,j)
			toV.addProperty('xpath', ConditionType.EQUALS, objectXpathV)
			WebUI.waitForElementPresent(toV, 30)
			WebUI.delay(3)
			WebUI.doubleClick(toV)
        }
    }
}

Here I try to iterate through the agenda table. Find the “cell” with the actual day and double click.
As I was having a lot of trouble trying to accomplish this, I create a new Test object and add its Id.
This is how I do it:

////div[@id='schedulerSch']/table/tbody/tr[2]/td/div/table/tbody/tr[5]/td[2]
	@Keyword
	def String createIdRelative(String id, int number1, int number2){
		//String numbWithZero = String.format('%04d', number)
		String num1 = number1.toString()
		String num2 = number1.toString()
		String xpath = id + num1 + "]/td[" + num2 + "]"
		return xpath
	}

But this wont work as it never finds that object with that id:
Unable to find the element located by ‘By.xpath: ////div[@id=‘schedulerSch’]/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td[2]’. Please recheck the objects properties to make sure the desired element is located.

This is the html of the agenda:

Do you have any suggestions on how to resolve this? Am I doing something wrong?

Where’s number2 ?

I’m not an xpath guru, but this CSS selector should target the td-element you highlighted in your screenshot:

div#schedulerSch .k-scheduler-content>table td:nth-child(5)

To prove it works, try something like this in the browser console:

document.querySelector("div#schedulerSch .k-scheduler-content>table td:nth-child(5)").style.outline = "thick solid orange"

I need more of that source code to help you. Can you attach the entire html source code

Thank you very much for the response.
Yess, my mistake…it should be like this:
String num1 = number1.toString()
String num2 = number2.toString()

I dont really understand what you are trying to tell me (sorry) but whenever I do that in the console of the browser it does something like this:
orange

Perfect. It proves you can target the cell you highlighted here:

I suspect the thing you want to click on is inside that td-element. Open it and post a screenshot.

Now try this in the browser console:

document.querySelector("div#schedulerSch .k-scheduler-content>table td:nth-child(5) > span").click()

When you have proved that works, then you can place it in your Test Case. Your code above uses j as a cell index so something like this:

for (int j = 0; j < Cols.size(); j++) {
  if(...) {
    String js = '''
    document.querySelector("div#schedulerSch .k-scheduler-content>table td:nth-child(${j}) > span").click()
    '''
    WebUI.executeJavaScript(js, null)
    ...

For background on using CSS and JS in Groovy/TestCases:

Thanks once again for the response.
When I do that in the console it does not work. The thing is that I need to double click if not I would do something like Cols.get(j).click

Solved !
This line was wrong:
String objectXpathV = CustomKeywords.‘keywordPrueba.CustomFunction.createIdRelative’(’////div[@id=‘schedulerSch’]/table/tbody/tr[2]/td/div/table/tbody/tr[’, i,j)

It should be like this:
CustomKeywords.‘keywordPrueba.CustomFunction.createIdRelative’(’//div[@id=‘schedulerSch’]/table/tbody/tr[2]/td/div/table/tbody/tr[’, i,j)

1 Like

There is a plugin dedicated for Calendar, could this make your life easier in the future?

https://store.katalon.com/product/25/Calendar-Keywords

Thanks for the answer. I am not really sure if that plugin does what I need to do.

This is the code that I have right now

WebDriver driver = DriverFactory.getWebDriver()

WebElement Table = driver.findElement(By.className('k-scheduler-content'))

List<WebElement> Rows = Table.findElements(By.tagName('tr'))

println('Rows: ' + Rows.size())

println('Rows: ' + Rows.get(0).getText())

Date today = new Date()

String todaysDate = today.format('mm/dd/yy').substring(3, 5)
println(todaysDate + " ")
for (int i = 0; i < Rows.size(); i++) {
	List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))

	for (int j = 0; j < Cols.size(); j++) {
		if (Cols.get(j).getText().equals(todaysDate)) {
			println(i + " " + j + " ")
			TestObject toV = new TestObject()
			
			String objectXpathV = CustomKeywords.'keywordPrueba.CustomFunction.createIdRelative'('//div[@id=\'schedulerSch\']/table/tbody/tr[2]/td/div/table/tbody/tr[',
				i + 1, j + 1)

			toV.addProperty('xpath', ConditionType.EQUALS, objectXpathV)

			WebUI.waitForElementPresent(toV, 30)

			WebUI.delay(3)

			WebUI.doubleClick(toV)
		}
	}
}

It works but it does nothing. The frame that should appear whenever I clic toV does not appear

For some reason it only lets mi click if I refresh the page

hello you,

why you are not using query selector as Russ ping here

For the same reason I wrote in a previous comment. That solution was not working and what I need is a double click not a single click

hello,

check this
https://www.guru99.com/double-click-and-right-click-selenium.html

1 Like

Thanks a lot for the answer.
My solution or yours do not work, because in a certain point the page refreshes and it stops me from clicking no matter what method I use.
Why it does it? I do not know because this was not happening yesterday

hello you,

i guess that page will refresh cause there is done database read or write query (back end), not sure but this is my strong guess

Its a strange thing because I sometimes notice the page refreshes and sometimes I do not. In any of this cases, I cannot click. I will keep on searching what the problem is and let you know if I can solve it.