如何识别按钮类元素?

时间:2014-11-25 23:34:29

标签: python selenium element

我尝试了以下方法来识别元素,但是当我运行脚本时,我收到“找不到元素”消息。

方法1尝试:

self.driver.find_element_by_xpath("//button[text()='Adopt and Initial']").click()

方法2尝试:

self.driver.find_element_by_css_selector(".btn-primary.btn.left.item-alt").click()

按钮的HTML:

更新了元素的Html代码。这是为了docusign。

    <div class="dialog is-signature-mode" tabindex="0">
  <header class="dialog-header">
    <h1 class="dialog-title">
      <span class="item-alt" data-group="tagType" data-group-item="signature">Adopt Your Signature</span>
      <span class="item-alt" data-group="tagType" data-group-item="initials" data-selected="">Adopt Your Initials</span>
    </h1>
    <nav class="icons">
      <a class="close" data-action="cancelAdoptSignature">
        <i class="icon-close"></i>
      </a>
    </nav>
  </header>
  <section class="dialog-body">
    <article id="adopt">

        <header class="ds-title p">
          Confirm your name, initials, and signature.
        </header>
        <div class="full-name">
          <div class="wrapper">
            <label for="full-name">Full Name</label> <span class="error hidden">Name required</span>
            <br>
            <div class="text-input-wrapper">
              <input id="full-name" disabled="" value="QAAuto 01Dec2014_10.41.03" name="fullname" type="text" class="required text-input" maxlength="50">
            </div>
          </div>
        </div>
        <div class="initials">
          <div class="wrapper">
            <label for="initials">Initials</label> <span class="error hidden">Initials required</span>
            <br>
            <div class="text-input-wrapper">
              <input id="initials" disabled="" value="Q0" name="initials" type="text" class="required text-input" maxlength="50">
            </div>
          </div>
        </div>


      <div class="clear-float"></div>
    </article>
    <header class="tab-nav">
      <ul>
        <li><a href="#select-style" class="tab tab-select-style selected">Select Style</a></li>
        <li><a href="#draw" class="tab tab-draw">Draw</a></li>


      </ul>
    </header>

      <article id="select-style" class="tab-panel panel-select-style selected">
        <h4 class="normal">Preview <span class="error"></span></h4>
        <div class="signature-preview">
          <div class="signature"><img alt="" src="https://demo.docusign.net/Signing/image.aspx?ti=56b2faad38e7427a99defd1dfaa258ce&amp;insession=1&amp;i=asig150&amp;force=154&amp;s=QAAuto+01Dec2014_10.41.03&amp;f=7_DocuSign&amp;nochrome=0" height="75px" class="signature-img left">
<img alt="" src="https://demo.docusign.net/Signing/image.aspx?ti=56b2faad38e7427a99defd1dfaa258ce&amp;insession=1&amp;i=ainit150&amp;force=155&amp;s=Q0&amp;n=QAAuto+01Dec2014_10.41.03&amp;f=7_DocuSign&amp;nochrome=0" height="75px" class="initials-img right">
<div class="clear-float"></div></div>
          <a class="change-style">
            Change Style
          </a>
          <div class="clear-float"></div>
        </div>
      </article>


      <article id="draw" class="tab-panel panel-draw">
        <h4 class="normal">
          <span class="item-alt-inline" data-group="tagType" data-group-item="signature">Draw your signature</span>
          <span class="item-alt-inline" data-group="tagType" data-group-item="initials" data-selected="">Draw your initials</span>
          <span class="error"></span>
        </h4>
        <a class="clear" data-ds="clear">Clear</a>
        <div class="signature-draw signature">

        <div class="canvas-wrapper"><canvas class="canvas" width="0" height="0"></canvas><canvas class="canvas" width="0" height="0"></canvas></div></div>
      </article>



    <p class="legalese">By clicking Adopt and Sign, I agree that the signature and initials will be the electronic representation of my signature and initials for all purposes when I (or my agent) use them on documents, including legally binding contracts - just the same as a pen-and-paper signature or initial.</p>
    <hr>
    <button class="btn-primary btn left item-alt" data-group="tagType" data-group-item="signature" type="button" data-ds="submit" value="initials">Adopt and Sign</button>
    <button class="btn-primary btn left item-alt" data-group="tagType" data-group-item="initials" type="button" data-ds="submit" value="initials" data-selected="">Adopt and Initial</button>
    <button class="close left btn btn-default" type="button" data-action="cancelAdoptSignature">Cancel</button>
    <div class="clear-float"></div>
  <div class="styles"></div></section>
</div>

1 个答案:

答案 0 :(得分:0)

请在xpath下面尝试,让我知道会发生什么

assertTrue(driver.getPageSource().contains("Adopt and Initial"));
self.driver.find_element_by_xpath("//button[conatins(text(),'Adopt and Initial')]".click(); 

现在确保在命令之前使用断言,因为如果断言失败,则当前帧中不存在该元素。所以我们必须在执行操作之前切换正确的帧。 如果您尝试这个,请告诉我。