Watir Display Content from a table in a text file - ruby

I am using watir-webdriver, I am trying to write the content displayed in a table below (RATE CODE SELECTED NOT AVAILABLE THIS LOCATION OR CAR 138DE) into a text file but I can't get it to display the content. Any idea what i doing wrong ? Thanks in advance for your help.
errorMes = browser.select_list(:name => 'seamlessMessages').value
f3=File.open( 'Error Log - Conf Res.txt', 'a')
f3.puts "Cancel Reservation - FAILED - Res ID: " +tasid+" - " + CSAsite + " - Message: " +errorMes
f3.puts "\n"
f3.close
code
<TD><B>Error/Informational Messages</B></TD>
<TD><B>Number of Messages</B></TD>
<TD class="tableData"><span name="nbrOfMessages"/>1</span></TD>
<TD> </TD>
</TR>
<TR>
<TD height="1"></TD>
</TR>
</TABLE>
<TABLE width="100%" class="displaySubsection" cellpadding="0" cellspacing="0">
<TR>
<TD width="5" rowspan="3"></TD>
<TD width="800"></TD>
<TD width="5" rowspan="3"></TD>
</TR>
<TR>
<TD>
<select name="seamlessMessages" size="2" readonly tabIndex="-1" class="readonly wide"><option value="0" selected>RATE CODE SELECTED NOT AVAILABLE THIS LOCATION OR CAR 138DE </option></select>
</TD>

I assume your problem is that you are getting "0" instead of "RATE CODE SELECTED NOT AVAILABLE THIS LOCATION OR CAR 138DE".
When you do errorMes = browser.select_list(:name => 'seamlessMessages').value, it is returning the 'value' attribute of the selected option.
If you want the text, you need to do:
errorMes = browser.select_list(:name => 'seamlessMessages').selected_options.first.text

Yes you're right I was getting "0"
Just to let you know that when i tried
errorMes = browser.select_list(:name => 'seamlessMessages').selected_options.first.text
I got an error message in <main>': undefined methodtext' for "RATE CODE SELECTED
NOT AVAILABLE THIS LOCATION OR CAR 138DE":String (NoMethodError)
By removing .text it did work. Thanks a lot for your help on this.
errorMes = browser.select_list(:name => 'seamlessMessages').selected_options.first

Related

How can I load this "label" using importxml & xPath

I've been trying to get this to load into google spreadsheet, but no success so far:
http://www.aastocks.com/tc/stocks/analysis/company-fundamental/basic-information?symbol=00027
The, I have the formula as follows:
=importxml ("http://www.aastocks.com/tc/stocks/analysis/company-fundamental/basic-information?symbol=" & To_Text(A7),"//td[#id='sb2-last']/label[#id='SQ_Last']/following-sibling::label/text()")
Result:
The content imported is empty.
The html part:
<table>
<tbody>
<tr>
<td>現價<label id="SQ_Currency">(港元)</label></td>
<td id="sb2-last">
<label id="SQ_Last" class="cls">**57.500**</label>
</td>
</tr>
</tbody>
</table>
try:
=INDEX(IMPORTXML(
"http://www.aastocks.com/tc/stocks/analysis/company-fundamental/basic-information?symbol="&
TO_TEXT(A7), "//td[#class='mcFont cls']"), 26)

Unable to check checkbox in table cell

I have this table:
<iframe title="ManageLegs">
<table title="Leg Details">
<tr>
<th class="headerRow">
<div>PAX</div>
</th>
<th class="headerRow">
<div>Leg</div>
</th>
</tr>
<tr>
<td>
<input type="text"></input>
</td>
<td>
<input type="checkbox"></input>
</td>
</tr>
</table>
</iframe>
Accessing the textbox is working fine, but I can´t figure out how to check the checkbox.
I use the following code in my PageObject class:
in_iframe(:title => 'ManageLegs') do |frame|
table(:leg_details, title: 'Leg Details', :frame => frame)
text_field(:pax) { leg_details_element['1']['PAX'].text_field_element }
text_field(:aircraft) { leg_details_element['1']['Aircraft'].text_field_element }
text_field(:show_leg_on_quote_pdf) { leg_details_element['1']['Leg'].checkbox_element }
end
Which I am calling like this:
on(CharterInquirySavedPage).pax = "2"
on(CharterInquirySavedPage).check_show_leg_on_quote_pdf
It is working fine for setting the pax-textbox, but fails when trying to check the checkbox:
NoMethodError: undefined method `check_show_leg_on_quote_pdf' for #<CharterInquirySavedPage:0x00000004cfd420>
The text_field accessor is for text fields, which are input elements with type "text" and other text-like input fields such as "email".
Inputs of type "checkbox" are considered checkboxes. It is the checkbox accessor that creates the check_ method. In other words, you want the following definition for the checkbox:
checkbox(:show_leg_on_quote_pdf) { leg_details_element['1']['Leg'].checkbox_element }

Webdriver in Ruby, how to check elements exist

I am using Webdriver in Ruby and I want to verify three text exists on a page.
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>
How do I verify "test1", "test2" and "test3" presents on this page using
find_element
find_elements
getPageSource?
What is the best approach for it?
Thank you very much.
I would go with #find_elements method,because with other 2 options there will be a chance to get no such element exception.
First collect it in an array -
array = driver.find_elements(:xpath,"//table[#class='c1']//tr[#class='c2']//span[text()='test1']")
Now check the array size
"test1 is present" unless array.empty?
The same way we can test for test2 and test3.
Following sample code will help you to perform your task :
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

Not able to click on the nested anchor element using xpath of selenium webdriver

Please find the Html below :
<table class="data" id="filteredTable" cellpadding="0" cellspacing="1">
<tbody>
<tr class="rowLight">
<td class="lt4"><input name="ids" value="att1" type="checkbox"></td>
<td><a hfref= " link1" > foo </a>
</td>
<td>item1</td>
<td>item2</td>
<td>item3</td>
</tr>
<tr class="rowDark">
<td class="lt4"><input name="ids" value="att2" type="checkbox"></td>
<td><a hfref= " link2" > boo </a>
</td>
<td>item1</td>
<td>item2</td>
<td>item3</td>
</tr>
<tr class="rowLight">
<td class="lt4"><input name="ids" value="att3" type="checkbox"></td>
<td><a hfref= " link3" > bar </a>
</td>
<td>item1</td>
<td>item2</td>
<td>item3</td>
</tr>
Now I need to click on the link of bar. But my below Xpath not helping me to get into the bar as well. So any help how to be done the same.
I didn't give the html for the part //form[contains(#name,'filterset_FilterSetListForm')]/table[contains(#class,'contentBody')]/tbody/tr/td/table[contains(#class,'content')]/tbody/tr/td/. <~~ till this I am correct. Confusion start after <tr> from here /table[contains(#id,'filteredTable')]/tbody/tr
Part-II:
When there will be a match say bar , can their associated check box (s) be clicked?
Any help in this regard?
I am using selenium -web driver with Ruby 1.9.3 .
You can get the a element this way
/table[contains(#id,'filteredTable')]/tbody/tr/td/a[contains(text(),'bar')]
or if you want an exact match to the link text
/table[contains(#id,'filteredTable')]/tbody/tr/td/a[text()=' bar ']

Nokogiri next_element with filter

Let's say I've got an ill formed html page:
<table>
<thead>
<th class="what_I_need">Super sweet text<th>
</thead>
<tr>
<td>
I also need this
</td>
<td>
and this (all td's in this and subsequent tr's)
</td>
</tr>
<tr>
...all td's here too
</tr>
<tr>
...all td's here too
</tr>
</table>
On BeautifulSoup, we were able to get the <th> and then call findNext("td"). Nokogiri has the next_element call, but that might not return what I want (in this case, it would return the tr element).
Is there a way to filter the next_element call of Nokogiri? e.g. next_element("td")?
EDIT
For clarification, I'll be looking at many sites, most of them ill formed in different ways.
For instance, the next site might be:
<table>
<th class="what_I_need">Super sweet text<th>
<tr>
<td>
I also need this
</td>
<td>
and this (all td's in this and subsequent tr's)
</td>
</tr>
<tr>
...all td's here too
</tr>
<tr>
...all td's here too
</tr>
</table>
I can't assume any structure other than there will be trs below the item that has the class what_I_need
First, note that your closing th tag is malformed: <th>. It should be </th>. Fixing that helps.
One way to do it is to use XPath to navigate to it once you've found the th node:
require 'nokogiri'
html = '
<table>
<thead>
<th class="what_I_need">Super sweet text<th>
</thead>
<tr>
<td>
I also need this
</td>
<tr>
</table>
'
doc = Nokogiri::HTML(html)
th = doc.at('th.what_I_need')
th.text # => "Super sweet text"
td = th.at('../../tr/td')
td.text # => "\n I also need this\n "
This is taking advantage of Nokogiri's ability to use either CSS accessors or XPath, and to do it pretty transparently.
Once you have the <th> node, you could also navigate using some of Node's methods:
th.parent.next_element.at('td').text # => "\n I also need this\n "
One more way to go about it, is to start at the top of the table and look down:
table = doc.at('table')
th = table.at('th')
th.text # => "Super sweet text"
td = table.at('td')
td.text # => "\n I also need this\n "
If you need to access all <td> tags within a table you can iterate over them easily:
table.search('td').each do |td|
# do something with the td...
puts td.text
end
If you want the contents of all <td> by their containing <tr> iterate over the rows then the cells:
table.search('tr').each do |tr|
cells = tr.search('td').map(&:text)
# do something with all the cells
end

Resources