简体   繁体   English

如何使用 Selenium (Python) 从 HTML 中获取完整的日期值

[英]How to get full date value from HTML using Selenium (Python)

I am trying to get date from HTML using selenium.我正在尝试使用 selenium 从 HTML 中获取日期。 The problem is, that in the HTML (if viewed through the console in a browser) I see the full date like 01.01.2001, but selenium returns me 01.January what I should do, to get full date?问题是,在 HTML 中(如果通过浏览器中的控制台查看)我看到完整日期如 01.01.2001,但 selenium 返回给我 01.January 我应该怎么做,以获得完整日期?

The part of HTML(what I see in the browser console) looks like this: HTML 的部分(我在浏览器控制台中看到的)如下所示:

<div>
   <h4 class="status-param"> Name and Surname </h4>
   <h4 class="status-param"> Date of Birth: 01.01.2001 </h4>     <!--problem only with this -->
   <h4 class="status-param"> 12.12.2012 00:00 </h4>
</div>

my code我的代码

driver = webdriver.Chrome('chrome/chromedriver')
        driver.get(html)
        result = []
        try:
            elements = WebDriverWait(driver, 10).until(
                EC.presence_of_all_elements_located((By.CLASS_NAME, "status-param"))
            )

            for element in elements:
                result.append(element.text)
        finally:
            driver.quit()

I get the following我得到以下

'Name and Surname'
'Date of Birth:  01.January'
'12.12.2012'

use the below xpath :使用以下 xpath :

(//div/h4[@class='status-param'])[2]

and use it like this :并像这样使用它:

elements = WebDriverWait(driver , 10).until(EC.visibility_of_element_located((By.XPATH, "(//div/h4[@class='status-param'])[2]"))).get_attribute('innerHTML')
print(elements)

the above should only prints 01.01.2001以上应该只打印01.01.2001

Update 1 :更新 1:

You can simply break string into 2 parts ang grab the second part like this :您可以简单地将字符串分成两部分,然后像这样抓住第二部分:

print(elements.split("\\:")[1])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM