使用python selenium来节省时间d

2024-10-02 22:23:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的代码行来获取元素的使用时间。从输出中,我可以看到我的目标是正确的区域,并且utime属性在那里,但是我仍然收到None的输出。我已经多次尝试重新编写data-utime属性,以确保函数的格式正确。我错过了什么?你知道吗

代码:

   timeStampBox = post.find_element_by_css_selector('.fsm.fwn.fcg')
   timeStampBox = timeStampBox.find_element_by_class_name('_5pcq')

   print(timeStampBox.get_attribute('innerHTML'))
   print(timeStampBox.get_attribute('data-utime'))

输出:

<abbr title="Monday, September 4, 2017 at 6:11am" data-utime="1504530675" data-shorten="1" class="_5ptz"><span class="timestampContent" id="js_15">September 4 at 6:11am</span></abbr>
None

Tags: 代码nonedatagetby属性attributeelement
1条回答
网友
1楼 · 发布于 2024-10-02 22:23:40

abbr元素是timeStampBoxinnerHTML,但data-utime不是timeStampBox的属性。你知道吗

我是这样模仿你的情况的:

<html>
<body>
<div><abbr title="Monday, September 4, 2017 at 6:11am" data-utime="1504530675" data-shorten="1" class="_5ptz"><span class="timestampContent" id="js_15">September 4 at 6:11am</span></abbr></div>
</body>
</html>

div元素是abbr元素的容器。我可以假装它是你的timeStampBox元素。你知道吗

>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get('file://c:/scratch/temp.htm')

识别timeStampBox并获取其innerHTML。和之前一样,我得到了abbr元素。你知道吗

>>> timeStampBox = driver.find_element_by_tag_name('div')
>>> timeStampBox.get_attribute('innerHTML')
'<abbr title="Monday, September 4, 2017 at 6:11am" data-utime="1504530675" data-shorten="1" class="_5ptz"><span class="timestampContent" id="js_15">September 4 at 6:11am</span></abbr>'

data-utimeNone,因为timeStampBox中不存在此属性。你知道吗

>>> timeStampBox.get_attribute('data-utime')

但它在abbr里。你知道吗

>>> abbr = driver.find_element_by_tag_name('abbr')
>>> abbr.get_attribute('data-utime')
'1504530675'

我们故事的寓意:直接搜索abbr。你知道吗

相关问题 更多 >