【发布时间】:2014-07-06 19:00:35
【问题描述】:
我正在使用 Ruby 和 Selenium 网络驱动程序测试一个网络应用程序。我无法检查显示网页中单元格的内容。 我想得到的是td中的IP。
<td class="multi_select_column"><input name="object_ids" type="checkbox"
value="adcf0467-2756-4c02-9edd-bb83c40b8685" /></td>
<td class="sortable normal_column">Core</td>
<td class="sortable nowrap-col normal_column">r1-c4-b4</td>
<td class="sortable anchor normal_column"><a href="/horizon/admin/instances
/adcf0467-2756-4c02-9edd-bb83c40b8685/detail" class="">pg-gtmpg--675</a></td>
<td class="sortable normal_column">column_name</td><td class="sortable normal_column">
<tr
<ul>
<li>172.25.1.12</li>
</ul>
我使用 Firefox 插件 firepath 来获取 IP 的 Xpath。 它给出了“html/body/div[1]/div[2]/div[3]/form/table/tbody/tr[1]/td[6]/ul/li”,看起来是正确的。
但是我无法显示 IP。 这是我的测试代码;
#usr/bin/env ruby
#
# Sample Ruby script using the Selenium client API
#
require "rubygems"
require "selenium/client"
require "test/unit"
require "selenium/client"
begin
driver = Selenium::WebDriver.for(:remote, :url =>"http://dog.dog.jump.acme.com:4444/wd/hub")
driver.navigate.to "http://10.87.252.37/acme/auth/login/"
g_user_name = driver.find_element(:id, 'id_username')
g_user_name.send_keys("user")
g_user_name.submit
g_password = driver.find_element(:id, 'id_password')
g_password.send_keys("password")
g_password.submit
g_instance_1 = driver.find_element(:xpath, "html/body/div[1]/div[2]/div[3]/form/table/tbody/tr[1] /td[4]/a")
puts g_instance_1.text() <- here, I see the can see text
g_instance_2 = driver.find_elements(:xpath, "html/body/div[1]/div[2]/div[3]/form/table/tbody/tr[1] /td[6]/ul/li[1]")
放置 g_instance_2
output is <Selenium::WebDriver::Element:0x000000023c1700
输入 g_instance_2.inspect
output is :[#<Selenium::WebDriver::Element:0x22f3b7c6e7724d4a id="4">]
把 g_instance_2.class 输出:数组
输入 g_instance_2.count 输出:1
当 td 中没有 /a 时,它似乎不起作用。 我试过 puts g_instance_2.text、g_instance_2.text() 和许多其他的都没有成功。 我一定遗漏了一些明显的东西,但我没有看到它
ruby 1.9.3p0(2011-10-30 修订版 33570)[x86_64-linux] 开启 Linux ubuntu 3.8.0-34-generic #49~precise1-Ubuntu
我决定尝试使用 css 选择器而不是 xpath 的不同方法。
当我将以下 css 选择器插入 FirePath 窗口时,将选择所需的 html 部分。
g_instance_2 = driver.find_elements(:css, "table#instances tbody tr:nth-of-type(1) td:nth-of-type(6) ul li:nth-of-type(1)" )
问题和之前一样,我好像无法访问g_instance_2的内容
我试过了;
puts g_instance_2
g_instance_22 = [g_instance_2]
puts g_instance_22
两者都返回;
#<Selenium::WebDriver::Element:0x000000028a6ba8>
#<Selenium::WebDriver::Element:0x000000028a6ba8>
如何检查从远程网络服务器返回的值?
Python 会是更好的选择吗?
【问题讨论】:
标签: ruby selenium xpath html-table