Java 文本为“新建”的按钮的 Xpath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23676537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:38:11  来源:igfitidea点击:

Xpath for button having text as 'New'

javaseleniumxpathselenium-webdriver

提问by HemaSundar

In our application almost in every screen we have a button with text 'New', Here is the html source for one of the button:

在我们的应用程序中,几乎在每个屏幕上都有一个带有文本“New”的按钮,这是其中一个按钮的 html 源代码:

<button id="defaultOverviewTable:j_id54" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover" type="submit" name="defaultOverviewTable:j_id54" role="button" aria-disabled="false">
    <span class="ui-button-text ui-c">New</span>
</button>

I have tried using the below statement to click on the button:

我尝试使用以下语句单击按钮:

driver.findElement(By.xpath("//button[[@type, 'submit'] and [text()='New']]")).click();

But this was not working

但这不起作用

org.openqa.selenium.InvalidSelectorException: The given selector //button[[@type= 'submit'] and [text()='New']] is either invalid or does not result in a WebElement.

Currently I am using the below code to click on the button:

目前我正在使用以下代码单击按钮:

List<WebElement> allButt = driver.findElements(By.tagName("button"));
for (WebElement w : allButt)
{
    if (w.getText().matches("New"))
    {
        w.click();
        break;
    }
}

As I have almost 150 buttons in the page. Is there any other way?

因为我在页面中有将近 150 个按钮。有没有其他办法?

采纳答案by Ian Roberts

Your xpath syntax is wrong - you don't need the inner set of square brackets - but even if you fix this:

你的 xpath 语法是错误的——你不需要内部的方括号集——但即使你解决了这个问题:

//button[@type, 'submit' and text()='New']

it won't select what you want it to. The problem is that the "New" is not text contained directly in the button element but is inside a child span element. If instead of text()you just use .then you can check the whole string value of the element (the concatenation of all descendant text nodes at whatever level)

它不会选择你想要的。问题是“新建”不是直接包含在按钮元素中的文本,而是在子跨度元素内。如果不是text()您只是使用,.那么您可以检查元素的整个字符串值(任何级别的所有后代文本节点的连接)

//button[@type='submit' and contains(., 'New')]

Or check spaninstead of text():

或检查span而不是text()

//button[@type='submit' and span='New']

(submit buttons containing a span whose value is "New")

(提交包含值为“新建”的跨度的按钮)

回答by Stephan

Try this xpath instead:

试试这个 xpath:

//button[@type='submit']/span[.='New']

Demo

演示

http://www.xpathtester.com/xpath/ff393b48183ee3f373d4ca5f539bedf2

http://www.xpathtester.com/xpath/ff393b48183ee3f373d4ca5f539bedf2



EDIT

编辑

Following comment from @Ian Roberts, you can use the following xpath expression instead if the click on the button element is important:

根据@Ian Roberts 的评论,如果单击按钮元素很重要,您可以使用以下 xpath 表达式:

//button[@type='submit']/span[.='New']/..

回答by Sharfi

The very simple solution for the above issue is to use span with option contains(text(),'').

上述问题的非常简单的解决方案是使用带有选项 contains(text(),'') 的 span。

You can use the following xpath code

您可以使用以下 xpath 代码

//span[contains(text(),'New')]

//span[contains(text(),'New')]