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

the_coder
7541 gold badge4 silver badges13 bronze badges
asked Mar 13, 2017 at 9:18
1
  • Ravi, have you got chance to try the solution provided? Commented Mar 17, 2017 at 4:05

5 Answers 5

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"
invzbl3
1191 silver badge8 bronze badges
answered Mar 14, 2017 at 5:04
0
1

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')
answered Aug 25, 2021 at 22:42
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);
João Farias
11.2k2 gold badges21 silver badges41 bronze badges
answered Mar 13, 2017 at 11:02
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

João Farias
11.2k2 gold badges21 silver badges41 bronze badges
answered Mar 14, 2017 at 6:00
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

João Farias
11.2k2 gold badges21 silver badges41 bronze badges
answered Aug 1, 2019 at 13:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.