Reputation: 1
I saw there are are couple of questions similar to this but any works for me. I'm working with some java robot, right now i'm stack in one place :/ trying to pick up and click calender date with dynamic xpath
//*[@id="root"]/div/div[1]/div[2]/div[1]/div/div/div/div[3]/div/span/div/div/div/div/div/div[2]/div[2]/div[3]/div[3]/div[1]
element is like this
<div class="DayPicker-Day DayPicker-Day--isDisabled" tabindex="-1" role="gridcell" aria-label="Mo. 9. Nov 2020" aria-disabled="false" aria-selected="false">9</div>
I got this idea to finbd id using aria-label my code right now is
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.valueOf(params.getFraAar()));
cal.set(Calendar.MONTH, Integer.valueOf(params.getFraMnd()) - 1);
cal.set(Calendar.DAY_OF_MONTH, Integer.valueOf(params.getFraDag()));
cal.add(Calendar.DAY_OF_MONTH, 1);
SimpleDateFormat format1 = new SimpleDateFormat("EEE. d. MMM yyyy", Locale.GERMAN);
String fraDato = format1.format(cal.getTime());
WebElement fraDatoElement = driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[1]/div[2]/div[1]/div/div/div/div[2]/div[1]/div[2]/div[1]/div/div[1]/div"));
fraDatoElement.click();
WebElement fraDatoWeb = driver.findElement(By.xpath("//*[@id=\"root\" and contains(@class=\"DayPicker-Day\") and contains(@aria-label='" + fraDato + "')]"));
fraDatoWeb.click();
I don't know what to do :/ all the time i got No such element exception.
Upvotes: 0
Views: 413
Reputation: 98
Try not to use such xpaths as you described. Rather than that, use shortened, relative paths. It will prevent from getting NoSuchElementException
as DOM tree can change in any place.
Also, this xpath is wrong:
WebElement fraDatoWeb = driver.findElement(By.xpath("//*[@id=\"root\" and contains(@class=\"DayPicker-Day\") and contains(@aria-label='" + fraDato + "')]"));
should be:
WebElement fraDatoWeb = driver.findElement(By.xpath("//*[@id='root' and contains(@class, 'DayPicker-Day') and contains(@aria-label,'" + fraDato + "')]"));
Upvotes: 1