如何获取元素的xPath

时间:2019-05-30 08:53:17

标签: xpath

我正在尝试获取data-content_published_date属性的值,该属性是使用xPath撰写的文章的发布日期,但由于某些原因,我无法!

这是我尝试的方式:

//div[@id='page-segment-values']/@data-content_published_date

这是div标签:

<div id="page-segment-values" class="">
  <div class="keyvals" data-content_author_name="Eli Meixler" data-content_cms_id="5461956" data-content_headline="British Academic" data-content_modified_date="" data-content_published_date="2018-11-22T08:15:43.000Z" data-content_shown_on_platform="own" data-content_type="article" data-path="/5461956/british-academic-sentenced-spying-united-arab-emirates/" data-referrer="" data-search="" data-content_is_post="post" data-title="A British Academic Has Been Sentenced to Life in Prison on Espionage Charges in the United Arab Emirates" data-affiliate_link_count="0" data-content_cms_category="World" data-content_cms_tags="onetime|overnight|United Arab Emirates" data-content_cms_terms="World,onetime,overnight,United Arab Emirates" data-time_inc_brand="time.com" data-time_inc_application="front end" data-content_syndicated="false" data-content_syndicated_brand="" data-content_syndicated_url="" data-content_nlp_sentiment_label="negative" data-content_nlp_sentiment_score="-0.2" data-content_nlp_sentiment_magnitude="5.9" data-content_nlp_entities="Matthew Hedges" data-content_nlp_payload="{&quot;entities&quot;:[{&quot;type&quot;:&quot;PERSON&quot;,&quot;text&quot;:&quot;Matthew Hedges&quot;,&quot;relevance&quot;:0.57477295,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;WORK_OF_ART&quot;,&quot;text&quot;:&quot;British Academic Sentenced to Life on Spying Charges&quot;,&quot;relevance&quot;:0.02937225,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;LOCATION&quot;,&quot;text&quot;:&quot;British&quot;,&quot;relevance&quot;:0.029201617,&quot;disambiguation&quot;:{&quot;mid&quot;:&quot;\/m\/07ssc&quot;,&quot;wikipedia_url&quot;:&quot;https:\/\/en.wikipedia.org\/wiki\/United_Kingdom&quot;}},{&quot;type&quot;:&quot;PERSON&quot;,&quot;text&quot;:&quot;academic&quot;,&quot;relevance&quot;:0.020707963,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;OTHER&quot;,&quot;text&quot;:&quot;life&quot;,&quot;relevance&quot;:0.018888554,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;OTHER&quot;,&quot;text&quot;:&quot;spying charges&quot;,&quot;relevance&quot;:0.013851213,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;LOCATION&quot;,&quot;text&quot;:&quot;prison&quot;,&quot;relevance&quot;:0.0133453375,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;LOCATION&quot;,&quot;text&quot;:&quot;United Arab Emirates&quot;,&quot;relevance&quot;:0.011457251,&quot;disambiguation&quot;:{&quot;wikipedia_url&quot;:&quot;https:\/\/en.wikipedia.org\/wiki\/United_Arab_Emirates&quot;,&quot;mid&quot;:&quot;\/m\/0j1z8&quot;}},{&quot;type&quot;:&quot;OTHER&quot;,&quot;text&quot;:&quot;threats&quot;,&quot;relevance&quot;:0.009783207,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;EVENT&quot;,&quot;text&quot;:&quot;blowback&quot;,&quot;relevance&quot;:0.009783207,&quot;disambiguation&quot;:{}}],&quot;categories&quot;:[{&quot;label&quot;:&quot;\/Law &amp; Government\/Public Safety\/Crime &amp; Justice&quot;,&quot;score&quot;:0.64}],&quot;docSentiment&quot;:{&quot;magnitude&quot;:5.9,&quot;score&quot;:-0.2,&quot;label&quot;:&quot;negative&quot;},&quot;language&quot;:&quot;en&quot;}" data-content_nlp_categories="/Law &amp; Government/Public Safety/Crime &amp; Justice"></div>
</div>

任何人都知道为什么我不能获得它的价值?以及如何获得它?

2 个答案:

答案 0 :(得分:1)

您错过了嵌套的div元素,因此您试图获取外部data-content_published_date的{​​{1}}的值。

正确的XPath查询应该

div

或更准确地说

//div[@id='page-segment-values']/div/@data-content_published_date

答案 1 :(得分:1)

使用内部<div>元素和您要使用的属性。

//div[@class='keyvals']/@data-content_published_date