1

Here are two code segments that I'm using to search for dates from a Calendar that has "From Date" and "To Date".

        webDriver.findElement(By.xpath("//table/tbody/tr/a[contains(text(),'October 30')]")).click();

        webDriver.findElement(By.xpath("//table/tbody/tr/a[contains(text(),'October 31')]")).click();

Error message shows: Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//table/tbody/tr/a[contains(text(),'October 30')]"}

Code segment image: code segment Please kindly help. Thank you.

2 Answers 2

1

Two things to fix (at least):

  • the a element is not a direct child of tr, there is one more level - the td element
  • you need to check the title attribute and not the text()

Fixed version:

//table/tbody/tr/td/a[contains(@title, 'October 30')]

Or, with a strict equality check:

//table/tbody/tr/td/a[@title = 'October 30']

Or, avoiding checking the parents:

//a[@title = 'October 30']

Or, alternatively, you may also try going with a bit more concise CSS selector:

webDriver.findElement(By.cssSelector("a[title='October 30']")).click();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the feedback alecxe. It has solved the issue partially. I can now see that the From and To Date fields are being clicked but the "From Date" gets cleared in less than a second and when the report is produced for selected date range, the date range selected looks to be the "To Date" being applied for both From Date and "To Date" field.
@mkarim yeah, from what it sounds like, I think you should make the context-specific searches - meaning, instead of simply having //table/..., use //table[@id='MainContent_FromDate']/... for the from date, and similarly for the to date..hope that helps.
Sorry for the late reply. Using By.xpath I was able to resolve this. Here is the code for From Date: clickFromDate = webDriver.findElement(By.xpath("/html/body/form[@id='ctl01']/div[@class='container-fluid body-content']/div[@class='container']/div[@class='row'][2]/div[@class='col-sm-6'][1]/table[@id='MainContent_FromDate']/tbody/tr[3]/td[1]/a")); clickFromDate.click();
Code for To Date: WebElement clickToDate = webDriver.findElement(By.xpath("/html/body/form[@id='ctl01']/div[@class='container-fluid body-content']/div[@class='container']/div[@class='row'][2]/div[@class='col-sm-6'][2]/table[@id='MainContent_ToDate']/tbody/tr[3]/td[4]/a")); clickToDate.click();
1

Don't rely on dates. It is unstable because from date and to date are not fixed dates. It may change for each and every records.

1 Comment

Thanks for your feedback Rajkumar. Is there a good way to select and click a date field from a Calendar. For now I've used the xpath to resolve this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.