2017-02-17 57 views
0

我使用硒来抓取标签的href属性。但我的代码是不掠“/利弊/ 52698281”,因为它应该。Selenium不会抓取所选元素,而是通过Javascript加载的元素

是因为我的代码错了,或者是因为一些javascript动态加载另一个url?他可以吗?

下面是HTML:

 <article class="bi-bloc blocs clearfix bi-pro visited" id="bi-bloc-014805042600000000C0001" data-pjtoggleclasshisto="{&quot;idbloc&quot;: {&quot;id_bloc&quot;: &quot;014805042600000000C0001&quot;, &quot;no_sequence&quot;: &quot;&quot; }, &quot;klass&quot;:&quot;visited&quot; }"> 
      <div class="zone-bi"> 
       <a class="visible-phone mob-zone-pro pj-lb pj-link" data-pjsearchctx-sethref="" href="/pros/52698281" data-pjstats="{&quot;idTag&quot;:&quot;MOB-ZONE-PRO&quot;,&quot;pos&quot;:54,&quot;type_bi&quot;:&quot;pro&quot;,&quot;genreBloc&quot;:&quot;1&quot;,&quot;pjscript&quot;:&quot;xt_click({},'C','{%xtn2}','LR_BI::zone_identification::info{%pjstats.type_bi}::identification_pro','A');&quot;}"> 
        <span class="not-visible"> 
         XXXXXXXXXXX 
        </span> 
       </a> 

我使用此代码来获取href属性:

elements = driver.find_elements(:css, "article.bi-bloc div.zone-bi a.visible-phone") 
elements.each do |e| 
    p e.attribute("href") 
end 

下面是javascript代码,我认为,动态加载另一个URL(在我的终端的一个打印)。

<script type="text/javascript"> 
var pj_searchctx = { 
    "1989516432": { 
     "form": { 
      "quoiqui": "climatisation", 
      "ou": "paris-75", 
      "proximite": 0 
     }, 
     "search": { 
      "technicalUrl":"/annuaire/chercherlespros?quoiqui=climatisation&ou=paris-75&idOu=L07505600&page=3&contexte=BupKFuSlIjbFtxi68rty83eKL16bkxx3e0d5jKAkSaA%3D&proximite=0&quoiQuiInterprete=climatisation", 
      "breadcrumb": "Retour aux résultats", 
      "stats": { 
       "idTag": "VERS-LR-RESULTATS" 
      } 
     } 
    } 
}; 

任何想法,我该怎么办?

回答

0

既然你在你的CSS选择器使用直接孩子,请尝试使用这个代替你(与>):

"article.bi-bloc > div.zone-bi > a.visible-phone" 

这是你要找的元素相匹配更具体。