简体   繁体   中英

Can I use CSS selectors in Tritium for moving/copying elements?

I am trying to copy an element to a given CSS selector in Tritium.

The Tritum Spec lists the signature for copy_to as:

copy_to(Text %xpath)

http://tritium.io/simple-mobile/1.0.224#Node.copy_to(Text%20%25xpath)

I am trying to do:

copy_to(  CSS_SELECTOR )

For eg:

copy_to("#header")

I cant seem to get this to work.

Here is the Tritium Tester URL: http://tester.tritium.io/4193cf46a239b4ff440cf1b4c36fb703cd22a5a4

Unfortunately, that won't work because of the way CSS selectors work in Tritium.

According to the spec, CSS selectors are converted into XPath local searches, which means they are scoped.

html() {
  $("/html") {
    $$("#header > img") {
      add_class("logo")
    }
    $$("#content") {
      $("./div[@id='courses']"){
        $$("a") {
          attribute("href", "http://console.moovweb.com/learn/training/getting_started/generate")
        }
        copy_to(css('#header'), "before")
      }
    }
  }
}

In your example, your copy_to function is in the scope of $("./div[@id='courses']") , so it won't find the div#header in there.

You'll have to use an XPath selector like this: copy_to("/html/body/div[@id='header']","before")

See here: http://tester.tritium.io/5f0ae313a4f43038ee4adeb49b81236bfbc5f097

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM