Ruby 中的 Webdriver,如何检查元素是否存在 [英] Webdriver in Ruby, how to check elements exist

查看:24
本文介绍了Ruby 中的 Webdriver,如何检查元素是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ruby 中使用 Webdriver,我想验证页面上是否存在三个文本.

I am using Webdriver in Ruby and I want to verify three text exists on a page.

这是我要验证的一段 html:

Here is the piece of html I want to verify:

<table class="c1">
  <thead>many subtags</thead>
  <tbody id="id1">
    <tr class="c2">
        <td class="first-child">
            <span>test1</span>
        </td>
        manny other <td></td>
    </tr>
    <tr class="c2">
        <td class="first-child">
            <span>test2</span>
        </td>
        manny other <td></td>
    </tr>
    <tr class="c2">
        <td class="first-child">
            <span>test3</span>
        </td>
        manny other <td></td>>
</tr>
  </tbody>
</table>

如何使用

  • find_element
  • find_elements
  • getPageSource?

最好的方法是什么?

非常感谢.

推荐答案

我会使用 #find_elements 方法,因为使用其他 2 个选项将有机会获得没有这样的元素例外.

I would go with #find_elements method,because with other 2 options there will be a chance to get no such element exception.

先收集到一个数组中-

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

现在检查数组大小

"test1 is present" unless array.empty?

我们可以用同样的方式测试 test2 和 test3.

The same way we can test for test2 and test3.

这篇关于Ruby 中的 Webdriver,如何检查元素是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆