在Ruby注入的JavaScript中转义撇号?

在Ruby注入的JavaScript中转义撇号?,javascript,ruby,Javascript,Ruby,我有以下代码行: page.execute_script(%Q($("div.fu-symptom-question:contains('Could your symptoms be associated with swimming?') label:contains('I have been swimming, but I don't think they are related') input").click())) “不要”这个词中的撇号给我带来了麻烦。我试过各种方法来逃避它,但我似乎做

我有以下代码行:

page.execute_script(%Q($("div.fu-symptom-question:contains('Could your symptoms be associated with swimming?') label:contains('I have been swimming, but I don't think they are related') input").click()))
“不要”这个词中的撇号给我带来了麻烦。我试过各种方法来逃避它,但我似乎做不好

我是一个Ruby noob,没有太多的JavaScript启动经验,所以我有点不适应这里的环境。

使用反斜杠

page.execute_script(%Q($("div.fu-symptom-question:contains('Could your symptoms be associated with swimming?') label:contains('I have been swimming, but I don\\'t think they are related') input").click()))

您可能一直在使用
\'
,它只是在Ruby中转义了引号。您需要插入一个反斜杠,
\\
,以便在JavaScript中转义引号。

感谢您的响应,不幸的是,当我尝试时,页面上仍然没有找到任何匹配项。如果我缩短“我一直在游泳,但我不认为它们与”我一直在游泳,但我“相关,它会找到正确的页面元素。所以我知道单报价仍然是个问题。如果两个报价失败,请尝试四个!