Java 找出类名是否包含某些文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20247327/
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-13 00:34:18  来源:igfitidea点击:

Find out if class name contains certain text

javacssxpathselenium

提问by user3006009

As part of my test, the system is supposed to find out if the device being used to open the website is a mobile device or a normal desktop.

作为我测试的一部分,系统应该确定用于打开网站的设备是移动设备还是普通桌面。

I keep getting the error:

我不断收到错误:

"InvalidSelectorError: Unable to locate an element with the xpath expression //*[contains(@class, is-mobile..."

“InvalidSelectorError: 无法定位带有 xpath 表达式的元素 //*[contains(@class, is-mobile...”

Property from firebug:

来自萤火虫的属性:

<body class="login-page is-desktop">

My test:

我的测试:

public class isMobile {

public static boolean isMobile = false;

public static boolean checkIfMobile(WebDriver driver) throws Exception {

    List<WebElement> list = driver.findElements(By
            .xpath("//body[contains(@class, 'is-mobile'"));
    if (list == null) {
        return false;
    } else {
        return true;
    }
}
}

Can somebody please tell me how the correct XPath should be?

有人可以告诉我正确的 XPath 应该如何吗?

采纳答案by Mosty Mostacho

You seem to be missing the closing round and square brackets:

您似乎缺少结束圆括号和方括号:

Change this:

改变这个:

//body[contains(@class, 'is-mobile'

Into this:

进入这个:

//body[contains(@class, 'is-mobile')]

As a side note, take into account that this code has another slightly hidden issue and it is that you will match things that you do not want to match such as this class attribute: login-page not-is-mobile.

作为旁注,请考虑到此代码还有另一个稍微隐藏的问题,即您将匹配不想匹配的内容,例如此类属性:login-page not-is-mobile

There is no way to simply match that as you would using the CSS3 selector [class~="is-mobile"]. However, you can come around this by doing this:

没有办法像使用 CSS3 选择器那样简单地匹配它[class~="is-mobile"]。但是,您可以通过执行以下操作来解决此问题:

//body[contains(concat(' ', @class, ' '), ' is-mobile ')]

All those spaces are there to make sure you will match only something between spaces and that it also matches even if it is at the beginning or end of the class attribute.

所有这些空格都是为了确保您只匹配空格之间的某些内容,并且即使它在类属性的开头或结尾也匹配。

It is ugly indeed but that is the XPath way of doing this.

这确实很丑陋,但这是 XPath 这样做的方式。

回答by Ross Patterson

This is a great example of using the wrong tool. One of the things that the HTML designers got wrong was using a single classstring attribute to encode multiple values. Unfortunately, the XPath designers, who at least had an array construct available to them, failed to remedy this error. So don't use XPath for this, even though, as Mosty Mostacho says, you can make it owork.

这是使用错误工具的一个很好的例子。HTML 设计者弄错的一件事是使用单个class字符串属性来编码多个值。不幸的是,至少有一个数组结构可供他们使用的 XPath 设计者未能纠正这个错误。所以不要为此使用XPath,尽管正如Mosty Mostacho 所说,您可以让它不工作。

The CSS designers did a better job. Although they didn't do something array-like, they at least elevated the classabove a simple string. Use By.cssSelector('body.is-mobile')- it's a proper use of CSS locators.

CSS 设计师做得更好。虽然他们没有做类似数组的事情,但他们至少将class上面提升了一个简单的字符串。使用By.cssSelector('body.is-mobile')- 这是 CSS 定位器的正确使用。