I am testing https://www.policybazaar.com using selenium, testNG, java. I get the no such element error while trying to select a date from date picker on https://www.policybazaar.com/travel-insurance/student/ .
While experimenting with xpath ( "//div[@data-time='1687458600000']"): I am properly detecting the element in browser. When used in code, I am getting the error.
Relevant code: WebElement initialization
@FindBy(xpath = "//div[@data-time='1687458600000']")
private WebElement endDay;
@FindBy(xpath = "//input[@id=\"startdate\"]")
private WebElement datePicker;
Method for choosing date (datepicker.click() works and datepicker appears on screen)
public void chooseDate() throws InterruptedException {
datePicker.click();
Thread.sleep(3000);
endDay.click();
}
Test method:
@Test(dependsOnMethods = "testSelectDestination")
public void testChooseDates() throws InterruptedException {
policyBazaarPage.chooseDate();
}
Part of Error Trace:
FAILED: tests.PolicyBazaarLandingTests.testChooseDates
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@data-time='1687458600000']"}
(Session info: MicrosoftEdge=113.0.1774.57)
For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element
Build info: version: '4.9.1', revision: 'eb2032df7f'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.15'
Driver info: org.openqa.selenium.edge.EdgeDriver
Command: [a54fc9488a04affd75b9e7bfc894b05e, findElement {using=xpath, value=//div[@data-time='1687458600000']}]
I had not noticed data-time changed at every refresh/launch, which caused the NoSuchElementException
. Changing the xpath, to eg: xpath = "//div[text()='12']"
,helped in easily selecting the desired date.