Ruby中的Webdriver,如何检查元素是否存在

我在Ruby中使用Webdriver,我想validation页面上是否存在三个文本。

这是我要validation的html片段:

many subtags manny other  manny other  manny other > 
test1
test2
test3

如何使用在此页面上validation“test1”,“test2”和“test3”

  • find_element
  • find_elements
  • getPageSource?

它的最佳方法是什么?

非常感谢你。

我会使用#find_elements方法,因为有了其他2个选项,就有机会得不到这样的元素exception

首先在arrays中收集它 –

 array = driver.find_elements(:xpath,"//table[@class='c1']//tr[@class='c2']//span[text()='test1']") 

现在检查数组大小

 "test1 is present" unless array.empty? 

我们可以测试test2和test3的方式相同。

以下示例代码将帮助您执行以下任务:

  def check_element_exists arr = ["test1", "test2", "test3"] arr.each do |a| if $driver.page_source.include? a puts a else print a puts " Not present" end end