【问题标题】:Selenium not grabbing the selected element but the one loaded by JavascriptSelenium 没有抓取选定的元素,而是 Javascript 加载的元素
【发布时间】:2017-02-17 11:32:49
【问题描述】:

我正在使用 selenium 来获取 a 标签的 href 属性。但是我的代码并没有像它应该的那样抓取“/pros/52698281”。

是因为我的代码错误还是因为某些 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

我认为这是动态加载另一个 url(在我的终端中打印的那个)的 javascript 代码。

<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"
            }
        }
    }
};

知道我该怎么做吗?

【问题讨论】:

    标签: ruby selenium


    【解决方案1】:

    由于您在 CSS 选择器中使用直接子代,请尝试使用它而不是您的(使用 &gt;):

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

    这更具体地匹配您要查找的元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      相关资源
      最近更新 更多