Paul Palmer
Paul Palmer

Reputation: 33

ruby selenium xpath td css

I am testing a webapp using Ruby and Selenium web-driver. I have not been able to examine the contents of a cell in the displayed webpage. What I would like to get is the IP in the td.

<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>

I used the Firefox addon firepath to get the Xpath of the IP. It gives "html/body/div[1]/div[2]/div[3]/form/table/tbody/tr[1]/td[6]/ul/li", which looks correct.

However I have not been able to display the IP. Here is my test code;

#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]")

puts g_instance_2

 output is <Selenium::WebDriver::Element:0x000000023c1700

puts g_instance_2.inspect

 output is :[#<Selenium::WebDriver::Element:0x22f3b7c6e7724d4a id="4">]

puts g_instance_2.class Output: Array

puts g_instance_2.count Output:1

When there is no /a in the td it doesn't seem to work. I have tried puts g_instance_2.text, g_instance_2.text() and many others with no success. I must be missing something obvious, but I am not seeing it


ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux] on Linux ubuntu 3.8.0-34-generic #49~precise1-Ubuntu

I decided to try a different apporach using the css selector instead of xpath.

When I insert the following css selector into the FirePath window the desired html section is selected.

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)" )

The problem is the same as before, I dont seem to be able to access the contents of g_instance_2

I have tried;

puts g_instance_2
g_instance_22 = [g_instance_2]
puts g_instance_22

Both return;

#<Selenium::WebDriver::Element:0x000000028a6ba8>
#<Selenium::WebDriver::Element:0x000000028a6ba8>

How can I check the value returned from the remote web-server?

Would Python be a better choice to do this?

Upvotes: 0

Views: 910

Answers (2)

Paul Palmer
Paul Palmer

Reputation: 33

The problem was that I was not accessing the returned array properly.

puts g_instance_2[0].text()

works for css and xpath

Upvotes: 0

Gabriel Cliseru
Gabriel Cliseru

Reputation: 99

The HTML code fragment you are trying to test is not valid HTML. It might be worth filing a bug report for it.

With the given code, the following CSS selector retrieves the <a> you want:

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

Translated into: any element that has the "href" attribute starting with "/horizon/admin/instances"

For XPATH this is the selector

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

Same translation just an uglier syntax.

Upvotes: 0

Related Questions