0

I'm writing some automated tests with Cucumber and Selenium. Currently, I'm trying to write a function to select the passed date in a calendar picker. I need to use the current year that is displayed in the calendar when it pops up to determine some logic.

However, I can't for the life of me figure out the correct syntax to get the value of the current year into Java to work with it further. The first div is the reliably non-ambiguous tag by which I navigate to the calendar that I need to work with, I can even somehow select the year(in this case "2017") in the browser dev console, but I can't pass it into Java.

Relevant HTML:

<ib-selector class="float-rg fblock40" ng-if="!hideYearSelector" initial-label="dateYear" next-handler="nextYear()" prev-handler="prevYear()">
  <div class="calendar-month-year">
    <span class="arrow-cal-left" ng-click="prevHandler()" role="button" tabindex="0">
  </span> 2017
    <span class="arrow-cal-right" ng-click="nextHandler()" role="button" tabindex="0">
  </span>
  </div>
</ib-selector>

Latest attempt at extracting the year:

String currentYear = driver.findElement(By.xpath("//ib-selector[@initial-label='dateYear']/div/text()")).getAttribute("data");

Exception it generates:

org.openqa.selenium.InvalidSelectorException: invalid selector: The result of the xpath expression "//ib-selector[@initial-label='dateYear']/div/text()" is: [object Text]. It should be an element.
1
  • 1
    html is not relevant to your xpath Commented Oct 25, 2017 at 9:47

2 Answers 2

2

with xpath (assuming that your's path is OK, as HTML in exmple actually is not relevant):

String text = driver.findElement(By.xpath("//ib-selector[@initial-label='dateYear']/div")).getText()

or if you want to use element:

WebElement el = driver.findElement(By.xPath("//ib-selector[@initial-label='dateYear']/div"));
String text = el.getText();

here is path relevant to HTML from the example:

 String text = driver.findElement(By.cssSelector(".calendar-month-year")).getText()
Sign up to request clarification or add additional context in comments.

Comments

2

Try to remove the "/text()" part in your xpath expression.

Comments

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.