我如何找出某些<li>元素<ul>中的数字?硒中

时间:2019-08-07 10:12:11

标签: selenium selenium-webdriver xpath webdriver robotframework

我有一个

    ,其中
  • 个元素的数量未知。每个
  • 都有一些文本和一个按钮。我该如何确定哪个
  • 包含我要查找的文本,以便单击同一
  • 中的按钮?

    <ul>  
      <li class="ot-queryitem row-fluid">
        <div class="ot-queryitem__info ot-queryitem__info--left span7">
          <span class="ot-queryitem__name">Äänikysely (Voice Activity and Participation Profile)</span>
        </div>
        <div class="ot-queryitem__info ot-queryitem__info--center span3"></div>
        <div class="ot-queryitem__info ot-queryitem__info--right span2">
        <button class="ot-button ot-button--undefined pull-right ot-queryitem__button">
          <i class="icon-pencil fa ot-icon ot-icon--default" style="margin-right: 8px;"></i>
          <span>Täytä</span>
        </button>
        <span class="ot-queryitem__logo"></span></div></li>
      <li class="ot-queryitem row-fluid">
        <div class="ot-queryitem__info ot-queryitem__info--left span7">
          <span class="ot-queryitem__name">Nikotiiniriippuvuustesti</span>
        </div>
        <div class="ot-queryitem__info ot-queryitem__info--center span3"></div>
        <div class="ot-queryitem__info ot-queryitem__info--right span2">
        <button class="ot-button ot-button--undefined pull-right ot-queryitem__button">
          <i class="icon-pencil fa ot-icon ot-icon--default" style="margin-right: 8px;"></i>
          <span>Täytä</span>
        </button>
        <span class="ot-queryitem__logo"></span></div></li>
    

5 个答案:

答案 0 :(得分:2)

  1. //span[text()='Nikotiiniriippuvuustesti']会给您一个<span>并带有给定的文字
  2. /ancestor::li轴将为您提供父级<li> tag
  3. /descendant::button轴会为您提供您要寻找的<button>
  4. 将所有内容放在一起:

    //span[text()='Nikotiiniriippuvuustesti']/ancestor::li/descendant::button
    

演示:

enter image description here

参考文献:

答案 1 :(得分:0)

尝试以下xpath。

xpath: //ul//li[contains(.,'Nikotiiniriippuvuustesti')]//button

OR

 xpath: //ul//li[contains(.,'Äänikysely')]//button

不过,您可以在robotframework中将此NikotiiniriippuvuustestiÄänikysely作为字符串变量传递。希望有帮助。

答案 2 :(得分:0)

要识别li标签,其中包含您要查找的文本-

${YOUR_TEXT}        Nikotiiniriippuvuustesti

## this keyword will give first matching element
${elements}=       Get WebElement    xpath://span[contains(text(),'${YOUR_TEXT}')]/ancestor::li

### wait for element to be present on page 
Wait Until Page Contains Element    ${elements}

## now click on button inside li tag found in above lines
Click Button    xpath://span[contains(text(),'${YOUR_TEXT}')]/ancestor::li//button 

注意:定位符取自您提供的html示例

答案 3 :(得分:0)

带有文本的元素和按钮都是每个<li>下的子元素。您需要从文本中查找HTML树,然后在同级元素下查找按钮

//div[span[contains(., 'Äänikysely')]]/following-sibling::div/button

答案 4 :(得分:-1)

您不必计算var generateSignature = function(callback, params_to_sign){ $.ajax({ url : signature_endpoint_url, type : "POST", dataType: "json", data : params_to_sign, complete: function() {console.log("complete")}, success : function(signature, textStatus, xhr) { callback(signature); }, error : function(xhr, status, error) { console.log(xhr, status, error); } }); } var widget = cloudinary.createUploadWidget({ cloud_name: cloud_name, api_key: api_key, upload_signature: generateSignature, upload_preset: preset_name, sources: ['local', 'url'], }, (error, result) => { }); <li>的数量。

根据文本单击相应按钮,例如Äänikysely Nikotiiniriippuvuustesti 等,您可以使用以下解决方案:

  • 要点击Äänikysely

    <ul>
  • 要单击 Nikotiiniriippuvuustesti

    Click Button    xpath://div[contains(@class, 'ot-queryitem__info--left')]/span[contains(.,'Äänikysely')]//following::button[@class='ot-button ot-button--undefined pull-right ot-queryitem__button']//span[text()='Täytä']