Find out if class name contains certain text

前端 未结 2 1303
悲哀的现实
悲哀的现实 2021-01-14 10:31

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:

2条回答
  •  终归单人心
    2021-01-14 10:50

    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.

    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.

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

提交回复
热议问题