3

how can i get the value "2017-03-11T04:50" from the below html using selenium webdriver.

<div class="game-date-time">
  <span data-date="2017-03-11T04:50Z" data-behavior="date_time">
  <span class="time game-time" data-dateformat="time1" data-showtimezone="true">10:20 AM IST</span>
  <span class="game-date" data-dateformat="date12">11 March 2017</span>
  </span>
</div>

Thanks in Advance

  • Ravi, have you got chance to try the solution provided? – Rao Mar 17 '17 at 4:05
3

Your div text does not provide value in the format as you mentioned ("2017-03-11T04:50")

To just get the value you can use xpath. Then you can parse the string to eliminate "Z". (if that is what you are looking for)

//get the attribute value first.

String date = driver.findElement(By.xpath("//div[@class='game-date-time']/span[@data-behavior='date_time']")).getAttribute("data-date");

// parsing for'z'.

String parts[] = date.split("z");

// to get fist portion.

String dateParsed = parts[0]; //which should be "2017-03-11T04:50"
0

In the code below, I have used xpath to find the element you want and have called getAttribute() method to get the attribute value of the tag.

String value = driver.findElement(By.xpath(".//div[@class='game-date-time']/span[@data-behavior='date_time']")).getAttribute("data-date");

.

String date = value.substring(0,value.length()-1);
0

You can simple get it by using below statement:

The xpath part gets the data string and removes Z, all in single statement.

String dateString = driver.findElement(By.xpath("substring-before(//div[@class='game-date-time']/span[@data-behavior='date_time']/@data-date,'Z')"));
System.out.println("Date string :"+dateString);

Online demo for the xpath

0

You can retrieve the value by using

driver.findElement(By.xpath("/html/body/span")).getAttribute("data-date");

Here is the sample code for thisenter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.