找出类名是否包含某些文本 [英] Find out if class name contains certain text

查看:154
本文介绍了找出类名是否包含某些文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我继续获取错误:


InvalidSelectorError:无法找到具有xpath表达式的元素// * [contains(@class,is-移动...

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

来自firebug的属性:

Property from firebug:

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

我的测试:

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;
    }
}
}

有人可以告诉我正确的XPath应该是什么? / p>

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

推荐答案

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

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

更改:

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

进入:

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

帐户,这个代码有另一个稍微隐藏的问题,它是你将匹配的东西,你不想匹配,如这个类属性: login-page not-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.

没有办法像使用CSS3选择器那样简单匹配 [class〜=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:

//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.

这是丑陋的,但这是XPath的做法这。

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

这篇关于找出类名是否包含某些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆