With selenium webdriver I need to get the text from a div tag which has a date in it how to go with that
here is the HTML tag showing the div tag.
<div class="gridlayout-column" style="padding-left: 10px; width: calc((100% - 110px) * 0.166667 + 10px);">
<div class="gridlayout-content">
<label class="fieldlabel">
<div class="fieldlabel-label">Last update</div>
<div class="fieldlabel-content">Jul 12, 2017</div>
</label>
</div>
</div>
I stored created a webelement for the above and tried using
WebElement lastUpdate = driver.findElement(By.xpath("//*[ . = 'Last update' and //div[contains(@class, 'fieldlabel-content')]]/following-sibling::div[@class='fieldlabel-content']"));
lastUpadte.getText();
The output was --
So I another option which is this
Date expiryDate = null;
DateFormat formatter = new SimpleDateFormat("MMM DD, YYYY");
String dateString = lastUpdate.getText();
try {
expiryDate = formatter.parse(dateString);
}
catch (ParseException e) {
e.printStackTrace();
}
Still it didn't work. I am sure I am not doing it properly so please help me in fixing this issue.
Try this below code to get the date
String date = driver.findElement(By.xpath("//div[@class='gridlayout-content']//div[@class='fieldlabel-content']")).getText();
System.out.println(date);
OR
you can do this way.
WebElement date = driver.findElement(By.xpath("//div[@class='gridlayout-content']//div[@class='fieldlabel-content']"));
String s = date.getText();
System.out.println(s);
I think the problem is your XPath is incorrect. You have a couple choices to fix the XPath expression.
First: Find the date element based on sibling text value 'Last update'.
WebElement lastUpdate = driver.findElement(By.xpath("//*[div='Last update']/*[@class='fieldlabel-content']");
Second: Find the date element by go into the sibling by text 'Last update' and then up to the parent and find the date.
WebElement lastUpdate = driver.findElement(By.xpath("//*[text()='Last update']/../*[@class='fieldlabel-content']");
You can get the date text by:
lastUpdate.getAttribute("innerText");
Finally, you need to change the date format to "MMM dd, yyyy"
.
DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.