黄瓜将找不到页面ID - Ruby,Capybara,Selenium

时间:2015-03-30 10:12:04

标签: selenium-webdriver cucumber capybara

我在这里遇到困难。我试图让Cucumber在页面上找到这个元素ID。首先要确认它的存在,然后再点击它。

<a id="ga_Vehicle alerts_Vehicle tax: Expired" href="/vehicles/fleetRecords?dol.expired=true&amp;filter=expired" class="alert-3-row" title="Vehicles with expired tax"></a>.

看起来ID已嵌入到超链接中。

当我尝试

Then /^I can verify that expired hyperlink is present within the alert$/ do
find_by_id "a_Vehicle alerts_Vehicle tax: Expired"
end

无法找到该元素。

我哪里错了?

干杯

2 个答案:

答案 0 :(得分:1)

根据您的HTML代码段,href属性的值为id="ga_Vehicle alerts_Vehicle tax: Expired"。您的定位器略有不同:"a_Vehicle alerts_Vehicle tax: Expired"

答案 1 :(得分:1)

您的ID不应包含空格。从这里开始:http://www.w3.org/TR/REC-html40/types.html#h-6.2

  

ID和NAME令牌必须以字母([A-Za-z])开头,后面可以跟任意数量的字母,数字([0-9]),连字符(&#34; - &#34 ;),下划线(&#34; _&#34;),冒号(&#34;:&#34;)和句号(&#34;。&#34;)。

空格不是ID中的有效字符,并且会彻底混淆任何dom解析器,jquery,CSS,selenium等。如果您的ID中确实需要那么多信息,您应该从ID中删除空格并用下划线替换它们。如果您使用该ID存储有关HTML节点的元数据,我建议不要这样做。

你可能想要的是将a_Vehicle和alerts_Vehicle之类的内容添加为类。