How can I match on an attribute that contains a certain string?

后端 未结 10 1985
一整个雨季
一整个雨季 2020-11-22 03:14

I am having a problem selecting nodes by attribute when the attributes contains more than one word. For example:

相关标签:
10条回答
  • 2020-11-22 03:56

    You can try the following

    By.CssSelector("div.atag.btag")

    0 讨论(0)
  • 2020-11-22 03:57

    mjv's answer is a good start but will fail if atag is not the first classname listed.

    The usual approach is the rather unwieldy:

    //*[contains(concat(' ', @class, ' '), ' atag ')]
    

    this works as long as classes are separated by spaces only, and not other forms of whitespace. This is almost always the case. If it might not be, you have to make it more unwieldy still:

    //*[contains(concat(' ', normalize-space(@class), ' '), ' atag ')]
    

    (Selecting by classname-like space-separated strings is such a common case it's surprising there isn't a specific XPath function for it, like CSS3's '[class~="atag"]'.)

    0 讨论(0)
  • 2020-11-22 04:02

    I came here searching solution for Ranorex Studio 9.0.1. There is no contains() there yet. Instead we can use regex like:

    div[@class~'atag']
    
    0 讨论(0)
  • 2020-11-22 04:12

    try this: //*[contains(@class, 'atag')]

    0 讨论(0)
提交回复
热议问题