Java 如何在SeleniumWebDriver中定义具有多个类的按钮的xpath

Java 如何在SeleniumWebDriver中定义具有多个类的按钮的xpath,java,eclipse,selenium,xpath,selenium-webdriver,Java,Eclipse,Selenium,Xpath,Selenium Webdriver,HTML弹出窗口上有两个按钮几乎相似。两个按钮都是相同的,除了跨度文本,没有什么不同 谁能建议一下,如何编写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()

HTML弹出窗口上有两个按钮几乎相似。两个按钮都是相同的,除了跨度文本,没有什么不同

谁能建议一下,如何编写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][条件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']
或者当您将其与包含一起使用时。 注意:如果需要,请确保您正在等待元素

对于具有多个类的元素,您有两个选项:

  • 使用@class='my\u class' 这意味着使用属性class=>my_类的整个值将是所有类,整个字符串

  • 使用包含(@class,“my_class”) 此选项允许您仅使用类属性值的一部分=>my_类可以是字符串的任何部分:ui、按钮、ui按钮等


  • 在您的例子中,您使用了
    按钮[@class='ui-button']
    ,这是不正确的,因为该按钮具有
    class=“ui-button ui-widget ui-state-default ui-corner-all-ui-button-text-only”

    检查这两个xpath是否有ok按钮

    //按钮/span[包含(text(),'OK')]


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

    @lauda.非常感谢,第三个选项对我很有用。我做了一些修改,比如//button[@class='ui-button ui小部件ui状态默认ui仅限所有ui按钮文本'][.//span[contains(text(),'OK')]]Nice。如果您使用//button[contains(@class,'ui button')][.//span[contains(text(),'OK')]]也应该可以,只是好奇一下,我的xpath有什么问题吗?是否需要定义所有按钮类?@lauda..非常感谢,现在我明白了区别:)