简体   繁体   English

使用Selenium Webdriver如何检索日期文本

[英]Using Selenium Webdriver how to retrive date text

With selenium webdriver I need to get the text from a div tag which has a date in it how to go with that 使用Selenium Webdriver我需要从div标签中获取文本,该标签中有一个日期,该如何处理

here is the HTML tag showing the div tag. 这是显示div标签的HTML标签。

<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并尝试使用

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 试试下面的代码来获取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. 我认为问题是您的XPath不正确。 You have a couple choices to fix the XPath expression. 您有几种选择来修复XPath表达式。

First: Find the date element based on sibling text value 'Last update'. 第一:基于同级文本值“ 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" . 最后,您需要将日期格式更改为"MMM dd, yyyy"

DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Selenium Webdriver 检索只读字段的值 - retrive value of read only field using Selenium Webdriver 如何使用Java和Selenium2 / Webdriver将明天的日期输入文本字段 - How can I enter tomorrow's date into a text field using Java and Selenium2/Webdriver 如何使用Selenium WebDriver处理动态文本(成功和失败文本) - How to handle dynamic text (Success and Failure text)using Selenium WebDriver 如何使用Selenium WebDriver从Excel中读取日期并返回它? - How to read Date from an Excel using Selenium WebDriver and return it? 如何使用硒webdriver在Datepicker中选择开始日期和结束日期? - How to select a Start and End date in a Datepicker using selenium webdriver? 如何使用 selenium webdriver 从日历中选择日期 - How to select date from calendar using selenium webdriver 如何通过与Java一起使用Selenium WebDriver从此HTML代码中获取文本 - How to get Text from this HTML code By using Selenium WebDriver with Java 如何使用Selenium Webdriver在联系人表单中输入文本? - How to Enter Text in Contact form using selenium Webdriver? 如何使用Selenium / WebDriver将文本输入TinceMCE编辑器 - How to input text into tinceMCE editior using selenium/webdriver 如何使用Selenium Webdriver识别特定文本上的语言 - How can I identify the language on a specific text using Selenium Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM