2014-07-06 44 views
0

我正在使用Ruby和Selenium Web驅動程序測試Web應用程序。我無法檢查顯示的網頁中單元格的內容。 我想得到的是td中的IP。ruby​​ selenium xpath td css

<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時,它似乎不起作用。 我試過把g_instance_2.text,g_instance_2.text()和其他許多人放在一起都沒有成功。 我必須失去了一些東西很明顯,但我沒有看到它


紅寶石1.9.3p0(2011-10-30修訂33570)x86_64的Linux的]上 Linux操作系統Ubuntu 3.8.0-34泛型#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會是一個更好的選擇嗎?

回答

0

您試圖測試的HTML代碼片段不是有效的HTML。可能值得爲它提交一個錯誤報告。

在給定的代碼,下面的CSS選擇器檢索<a>你想:

[href^="/horizon/admin/instances"] 

翻譯成:具有「href」屬性以「/地平線/管理/實例」

任何元素

對於XPATH這是選擇

("//a[contains(@href,'/horizon/admin/instances')]") 

相同的轉換隻是一個醜陋的語法。

+0

KerridgeO嗨,我不知道我的理解。您提供的XPATH會選擇具有/ horizo​​n/admin/instance屬性的所有節點。但是有幾行具有此屬性。我需要下一個單元格,我認爲識別它的唯一方法是通過表格中的行/列位置。我是XPATH的新手,所以請原諒我,如果它應該是明顯的。 –

+0

在提供的HTML代碼中,沒有下一個單元這樣的東西。一個單元格涉及內部的一個​​- http://www.w3schools.com/html/html_tables.asp。 這將打破錶: 「

  • 172.25.1.12
  • 」 的 「>」 是 「

    +0

    @PaulPalmer嗨,我只幫助格式化文本,這是Gabriel的回答:) – Kerridge0

    0

    問題是我沒有正確訪問返回的數組。

    puts g_instance_2[0].text() 
    

    作品的CSS和XPath