我可以在Tritium中使用CSS选择器来移动/复制元素吗?

Dou*_*ard 2 moovweb tritium

我试图将一个元素复制到Tritium中的给定CSS选择器.

Tritum Spec将copy_to的签名列为:

copy_to(Text %xpath)
Run Code Online (Sandbox Code Playgroud)

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

我想做:

copy_to(  CSS_SELECTOR )
Run Code Online (Sandbox Code Playgroud)

例如:

copy_to("#header")
Run Code Online (Sandbox Code Playgroud)

我似乎无法让这个工作.

这是Tritium Tester URL:http://tester.tritium.io/4193cf46a239b4ff440cf1b4c36fb703cd22a5a4

gre*_*eng 5

不幸的是,由于CSS选择器在Tritium中的工作方式,这将无法工作.

根据规范,CSS选择器被转换为XPath本地搜索,这意味着它们是作用域.

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")
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在您的示例中,您的copy_to函数属于范围$("./div[@id='courses']"),因此无法div#header在其中找到.

你必须使用这样的XPath选择器: copy_to("/html/body/div[@id='header']","before")

见这里:http://tester.tritium.io/5f0ae313a4f43038ee4adeb49b81236bfbc5f097