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– Rao2017年03月17日 04:05:35 +00:00Commented Mar 17, 2017 at 4:05
5 Answers 5
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"
It's may be late but recently I was attempting to get the values by div class name but I tried all solutions available but then I found following is currently works perfect if you want to select by class name which is a div class.
driver.find_element_by_css_selector('div.class_name')
for this example it would be
driver.find_element_by_css_selector('div.game-date-time')
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);
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
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