java
  • eclipse
  • selenium
  • xpath
  • selenium-webdriver
  • 2016-09-15 1 views 2 likes 
    2

    HTMLポップアップには2つのボタンがほとんど似ています。両方のボタンは、スパンテキストを除いて同じですが、何も違いはありません。セレンのwebdriverに複数のクラスを持つボタンのxpathを定義する方法

    誰でもお勧めします、OKボタンのxpathを書く方法はありますか?

    私は以下のxpathを書きましたが、システムにエラーが表示されました。

    私のXpath

    driver.findElement(By.xpath("//div[@class='ui-dialog-buttonset']/descendant::button[@class='ui-button']/span[contains(@class, 'ui-button-text') and text() = 'OK']")).click(); 
    

    エラーここ

    org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:  
    

    は、ポップアップの私のHTMLコードです:

    HTML

    <div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-dialog-buttons ui-draggable" style="height: auto; width: 350px; top: 295.5px; left: 621px; display: block; z-index: 102;" tabindex="-1" role="dialog" aria-describedby="DivExtension" aria-labelledby="ui-id-2"> 
        <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix ui-draggable-handle"> 
         <span id="ui-id-2" class="ui-dialog-title">内線番号</span> 
         <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" type="button" role="button" title="Close"> 
           <span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span> 
           <span class="ui-button-text">Close</span> 
         </button> 
        </div> 
    
        <div id="DivExtension" class="display-none ui-dialog-content ui-widget-content" style="display: block; width: auto; min-height: 0px; max-height: none; height: 68px;"> 
          <div id="extRow" class="display-none" style="display: block;"> 
           <p class="form-label"> 
            <label for="extension">内線番号</label> 
           </p> 
           <p class="form-input"> 
            <input id="Extension" class="text" type="text" placeholder="Extension" value=""> 
           </p> 
          </div> 
        </div> 
    
        <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"> 
         <div class="ui-dialog-buttonset"> 
           <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" style="height: 34px; width: 104px;" role="button"> 
            <span class="ui-button-text">OK</span> 
           </button> 
    
           <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" style="height: 34px; width: 124px;" role="button"> 
            <span class="ui-button-text">キヤンセル</span> 
           </button> 
         </div> 
        </div> 
    

    答えて

    1

    ページに複数のボタンがあり、OKのテキストが表示されているかどうかによって異なります。

    あなたはこの場合も
    // * [condition_1] [condition_2]

    のようなものを使用することができ、複数の条件を使用する場合:

    //span[@class='ui-button-text'][text()='OK'] 
    

    やボタンの:

    //button[@class='ui-button'][.//span[contains(text(), 'OK')]] 
    

    複数のクラスの例:

    //button[@class='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'] 
    

    またはcontainsと一緒に使用する場合。 注:必要に応じて要素を待っていることを確認してください。これは、属性クラス=> my_classすべてのクラスになります、全体の全体の値を使用することを意味

    1. 使用する@クラス=「my_class」 :複数のクラスを持つ要素について

      あなたは2つのオプションを持っていますUI、ボタン、UIボタン:文字列

    2. 使用は、このオプションを使用すると、クラス属性値の一部だけを使用することができます(@class、「my_class」) => my_classは、文字列の任意の一部とすることができる含まれてい

    button[@class='ui-button']を使用しましたが、ボタンにはclass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"が含まれているため正しくありません。[OK]ボタンの

    +0

    lauda..Thank @あなたは非常に多く、第三の選択肢は私のために動作します。 // button [@ class = 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only] [。 )、 'OK')]] – Swa

    +0

    ニース。 //ボタン[contains(@class、 'ui-button')] [.// span [contains(text()、 'OK')]] – lauda

    +0

    ちょうど好奇心、何が間違っている私のxpathで?すべてのボタンクラスを定義する必要はありますか? – Swa

    0

    チェックこれら二つのXPathのは

    //button/span[contains(text(),'OK')]

    OR

    //div[@class='ui-dialog-buttonset']/button/span[contains(text(),'OK')]

    関連する問題