Trouble selecting checkbox on website - ajax

after a couple of days of trying, and failing, i need some help please.
I'm trying to select a check box on an website, but it doesn't seems to work as I want to.
the check box is part of a row in a table, together with some other data. The idea is that I log in on the site (http://www.apf.asn.au), go to the table, find the numbers I need from my excel sheet, and select the box that is in the row the number is found.
the log in and navigate to table part is working ok, but I can't find the proper way to select the box.
code from the site:
<div class="table">
<div>
<table class="table APFGrid StudentsSearch"
id="ctl00_ContentPlaceHolderMainNoAjax_gvJumpRecordsList"
style="border-collapse: collapse;" border="1" rules="all" cellspacing="0"
<tbody>
<tr>...</tr> 'Header
<tr>
<td>
<span class="clsSelect">
<input
name="ctl00$ContentPlaceHolderMainNoAjax$gvJumpRecordsList$ctl02$chkSelect"
id="ctl00_ContentPlaceHolderMainNoAjax_gvJumpRecordsList_ctl02_chkSelect"
onclick="javascript:ToggleCheckBoxSelection;" type="checkbox"></input>
</span>
</td> 'Box need to check
<td class="Name">...</td>
<td class="Name">...</td>
<td class="DateBirth">...</td>
<td class="APFNum">...</td> 'Number I'm looking for
<td class="JumpType">...</td>
<td class="DateBooked">...</td>
<td>...</td>
<td class="Name">...</td>
</tr>
<tr>...</tr>
Etc.
I use this code:
'Get APF Number from Pipe
xNr = "20819334"
'Search Number online
Dim i As Integer
i = 0
For Each Row In APFtable
If i = 0 Then
Set Member = Row.getElementsByTagName("th")
Else
Set Member = Row.getElementsByTagName("td")
End If
i = i + 1
On Error Resume Next
If xNr = Member(4).innerText Then
'Check the corresponding checkbox
Exit For
End If
Next Row
to compare the number, in my example: xNr, to the numbers in the table. But I can't figure out how to check the box.
I tried this code:
Name = "ctl00$ContentPlaceHolderMainNoAjax$gvJumpRecordsList$ct" & 100 + i & "$chkSelect"
Set Check = IE.Document.GetElementsByName(Name)
If Not Check Is Nothing Then
Check(0).Click
End If
end all the other ways I could find in other forums to get it to work, although this came closest to working. which means that it worked when I hard-coded the name in the variable with out the "calculating position" part.
can someone advice me on my problem?
Much appreciated

Member(4).parentElement.getElementsByTagName("input")(0).Click
Should do it if the checkbox is the first input element on that row.

Related

correct way to scrape this table (using scrapy / xpath)

Given a table (unknown number of <tr> but always three <td>, and sometimes containing a strikethrough (<s>) of the first element which should be captured as additional item (with value 0 or 1))
<table id="my_id">
<tr>
<td>A1</td>
<td>A2</td>
<td>A3</td>
</tr>
<tr>
<td><s>B1</s></td>
<td>B2</td>
<td>B3</td>
</tr>
...
</table>
Where scraping should yield [[A1,A2,A3,0],[B1,B2,B3,1], ...], I currently try along those lines:
my_xpath = response.xpath("//table[#id='my_id']")
for my_cell in my_xpath.xpath(".//tr"):
print('record 0:', my_cell.xpath(".//td")[0])
print('record 1:', my_cell.xpath(".//td")[1])
print('record 2:', my_cell.xpath(".//td")[2])
And in principle it works (e.g. by adding a pipeline after add_xpath()), just I am sure there is a more natural and elegant way to do this.
Try contains :
my_xpath = response.xpath("//table[contains(#id, 'my_id')]").getall()

XPath to select specific text inside of text block

I am trying to figure out a way to pull specific values out of a big long text block.
So far I have //td[#class="PadLeft10"] which returns me a big long value starting with the company name and ending with the "View More Info" piece.
I am trying to break my results up into segments, so for example I want my code to look for the words "Primary Contact:" and then return the text that follows that, ending at the <br/>.
I need to get the Company Name, which is always the first bit of text, then the Primary Contact, then the Address, then the Phone and Fax, then the Website, and the Organization type.
The problem is that not every record has all the values. As you can see, the second entry has the address and website, but the first one doesn't.
I am using the Dataminer Chrome Plugin, for anyone familiar with that. It has separate xpath for rows and columns, so I am going to try to make a bunch of different columns that correspond to each of the fields that I am looking for.
Any direction would be greatly appreciated.
<td align="left" valign="top" width="2%">
<script>
if (0 == 1) document.write('<img src="https://website.com" border="0" alt=""/>');
</script>
<br/><br/></td>
<td class="PadLeft10" align="left" valign="top" width="32%" style="padding-left: 15px;">
<span style="font-weight: bold;font-size: 12pt;"><br/>Company Name Here</span><br/>Primary Contact: Mr. Eric Cartman <br/>Phone: (555) 555-5555<br/>Fax: (333) 333-3333<span style="text-decoration: underline;color: #0000ff"></span><br/>Organization Type: Distributor Branch
<br/>
» View More Info<br/>
<br/>
</td>
<td align="left" valign="top" width="2%">
<script>
if (0 == 1) document.write('<img src="https://website.com" border="0" alt=""/>');
</script>
<br/><br/></td>
<td class="PadLeft10" align="left" valign="top" width="32%" style="padding-left: 15px;">
<span style="font-weight: bold;font-size: 12pt;"><br/>Other Company</span><br/>Primary Contact: Mr. Jimmy Valmer<br/>100 N Ohio St 2rd Fl<br/>Rochester, IN 54225<br/>United States<br/>Phone: (888) 888-8888<br/>Fax: (999) 999-9999<span style="text-decoration: underline;color: #0000ff"><br/>Web Site: http://www.companywebsite.com</span><br/>Organization Type: Financial Service
<br/>
» View More Info<br/>
<br/>
</td>
</tr>
<tr>
I am new to xpath, but the least i can say: if you are the creator of the html code, you absolutely need to change it to be more structured
like : Primary Contact:<span id/class='primaryContact'>..</span>
Or else, you can get the elements by this selector (to edit) //td[#class="PadLeft10"]//child::span//following-sibling::text()[1] split by ':' and then proceed, but this solution stay just a diy.
Any direction would be greatly appreciated.
As far as a direction, the sections within table cell that you mention are neither nested DOM items, nor sibling-type DOM nodes. Those are sequential html elements that require special processing.
<br/>Company Name Here</span>
<br/>Primary Contact: Mr. Eric Cartman
<br/>Phone: (555) 555-5555
<br/>...
Both xpath and regex can be leveraged for such a case.
You can select the text node you're looking for using a predicate and the contains function:
//td[#class="PadLeft10"]/text()[contains(., "Primary Contact:")]
Then you can get the substring using the substring-after function:
substring-after(
//td[#class="PadLeft10"]/text()[contains(., "Primary Contact:")],
'Primary Contact:'
)
And remove leading and trailing whitespace using normalize-space:
normalize-space(
substring-after(
//td[#class="PadLeft10"]/text()[contains(., "Primary Contact:")],
'Primary Contact:'
)
)

Selecting checkbox from the line that contains word Capybara

I have been searching answer for week.
I need to create automatic tests and one of them has to delete a line from table. This line looks like this:
Html looks like this:
<tr>
<td colspan="1" class="name" rowspan="1">
Capybara852
</td>
<td colspan="1" class="description" rowspan="1">This whitelist is added by Capybara automated test</td>
<td colspan="1" class="whitelistType" rowspan="1">Internal</td>
<td colspan="1" class="status" rowspan="1">None</td>
<td colspan="1" class="active" rowspan="1">false</td>
<td colspan="1" class="msCount" rowspan="1">40</td>
<td colspan="1" class="modifiedBy" rowspan="1">admin</td>
<td colspan="1" class="modifiedOn" rowspan="1">26.06.2014 11:08</td>
<td colspan="1" class="selected" rowspan="1">
<input class="check" onclick="disableButton('delete', false);" id="check_0" name="check_0" type="checkbox">
<img id="check_0_icon" class="t-error-icon" alt="" src="/mpromoter/assets/4716a6a0a357181/core/spacer.gif" style="display: none;">
</td>
</tr>
I need to check this checkbox that stands in the same line where is written Capybara. I can't select this checkbox because its id may be different every time I run these automatic tests.
I am asking, how can I select checkbox without its own id or name or class, I need to select checkbox that stands in the same line that contains Capybara
It is a href part there.
I can select checkbox with its id. But I want to select the checkbox with unknown id from the line that contains the word capybara :)
I have tried many different things but nothing works...
So I am asking for some help. What I have to do, any suggestions?
I am not sure if I understand what are you asking but...
So you can take each row from your table and then check if the row has the value 'Capybara'
$('#stack tbody tr').find('td a').text().match(new RegExp('Capybara'))
If that is true, from the same row do what you want with the checkbox.
I need to do to many things to give you a capybara version, but you just need to translate that to capybara.
Hope that helps.
I would suggest the approach you use be:
Determine how you can find the row you want (ie contains "Capybara")
Use the within method to find the checkbox within that row
For example, you can find any row that contains the word "Capybara" anywhere using:
find('tr', :text => 'Capybara')
You can use these same find options in within. When finding the checkbox within the within block, Capybara will only search in that row. So if you do not care where the word "Capybara" shows up in the row, you can do:
within('tr', :text => 'Capybara') do
find('input.check').set(true)
end
If needed, you can change the within options to be more specific. For example, you might only want rows where the first column, which has class "name", has the word "Capybara" (rather than "Capybara" just being in the description column). This could be done with:
within(:xpath, '//tr[td[#class="name" and contains(., "Capybara")]]') do
find('input.check').set(true)
end

Watir: Search table cell by bgcolor tag and get column number

Consider the following html as an example. its a scratch sheet I made to practice, but it has a snippet of the real html I am trying to work with.
http://www.carbide-red.com/prog/test_table.html
I am trying to locate a column and the only consistant identifier I can find is the background color (bgcolor).
<tr bgcolor="#ffffcc">
<td bgcolor="yellow" class="date" align=center>Equipment</td>
<td bgcolor="#ccccff" align=center class="date"><font color=black>8/12/12</font></td>
<td bgcolor="#ccccff" align=center class="date"><font color=black>8/19/12</font></td>
<td bgcolor="#ccccff" align=center class="date"><font color=black>8/26/12</font></td>
<td bgcolor="#ccccff" align=center class="date"><font color=black>9/2/12</font></td>
<td bgcolor="red" align=center class="date"><font color=yellow>9/9/12</font></td>
<td bgcolor="#ccffcc" align=center class="date"><font color=black>9/16/12</font></td>
<td bgcolor="#ccffcc" align=center class="date"><font color=black>9/23/12</font></td>
<td bgcolor="#ccffcc" align=center class="date"><font color=black>9/30/12</font></td>
<td bgcolor="#ccffcc" align=center class="date"><font color=black>10/7/12</font></td>
</tr>
I'm trying to find the <td> that has bgcolor=red. I would then like to save the column index of that cell, so that I can then use it to select the same column of the following rows.
But I can't seem to find a way to search for the bgcolor= tag. And I have not been able to find a way to get Watir to report back the column/row indexs to store in a variable. But if I can find the bgcolor= tag then I can search for like "equipment" and then count until I find the correct tag.
I know the html code is not ideal due to there note being any "name" or anything unique identifier, but I can't change that.
I am very new to Ruby & Watir. I tried to manipulate a website in Perl and it was was not going very well, and I discovered Watir and it did exactly what I needed (and suprisingly easy), but now I am trying to understand Ruby as well as the finer semantics.
Thanks for any help!
To get text of <td bgcolor="red"> try this:
browser.element(:css => "td[bgcolor=red]").text
You should get back "9/9/12". To click the element, replace text with click.
To put it's index in variable index try this:
index = nil
browser.tds.each_with_index {|td, i| index = i if td.attribute_value("bgcolor") == "red" or td.attribute_value("bgcolor") == "#ff0000"}
index variable should be 5.
I would use nokogiri if I were you:
doc = Nokogiri::HTML #browser.html
td = doc.at('td[#bgcolor="red"]')
index = td.search('./preceding-sibling::td').length
Unless there's tricky javascript on the page you're probably better off with mechanize than watir.
Yes the webpage I'm dealing with uses Javascript which is why I had a very hard time useing Mechanize::Firefox under Perl. Watir worked much more smoothly.
Thank you for your suggestion! It didn't work at first, but it helped me with Google searches and I was able to get a working version.
require "watir"
require "nokogiri"
browser = Watir::Browser.new
browser.goto "http://www.carbide-red.com/prog/test_table.html"
doc = Nokogiri::HTML.parse(browser.html)
td = doc.at('td[#bgcolor="red"]')
columnindex = td.search('./preceding-sibling::td').length
puts columnindex
browser.close
This returned "5"
Update:
For the sake of others who may find this while searching and learning. To use columnindex variable to find a specific column within a row use this code:
textvariable = browser.td(:text => "A58004").parent.td(:index => "#{columnindex}").text
puts "Textvariable: #{textvariable}"
This finds a <td> that contains the term "A58004" and then goes to the 5th column (0-5) over and returns the value of that cell. Using the webpage linked in my original question that would be "W=Sa"

XPath matching text in a table - Ruby - Nokigiri

I have a table that looks like this
<table cellpadding="1" cellspacing="0" width="100%" border="0">
<tr>
<td colspan="9" class="csoGreen"><b class="white">Bill Statement Detail</b></td>
</tr>
<tr style="background-color: #D8E4F6;vertical-align: top;">
<td nowrap="nowrap"><b>Bill Date</b></td>
<td nowrap="nowrap"><b>Bill Amount</b></td>
<td nowrap="nowrap"><b>Bill Due Date</b></td>
<td nowrap="nowrap"><b>Bill (PDF)</b></td>
</tr>
</table>
I am trying to create the XPATH to find this table where it contains the test Bill Statement Detail. I want the entire table and not just the td.
Here is what I have tried so far:
page.parser.xpath('//table[contains(text(),"Bill")]')
page.parser.xpath('//table/tbody/tr[contains(text(),"Bill Statement Detail")]')
Any Help is appreciated
Thanks!
Your first XPath example is the closest in that you're selecting table. The second example, if it ever matched, would select tr—this one will not work mainly because, according to your example, the text you want is in a b node, not a tr node.
This solution is as vague as I could make it, because of *. If the target text will always be under b, change it to descendant::b:
//table[contains(descendant::*, 'Bill Statement Detail')]
This is as specific, given the example, as I can make:
//table[tr[1]/td/b['Bill Statement Detail']]
You might want
//table[contains(descendant::text(),"Bill Statement Detail")]
The suggested codes don't work well if the match word is not in the first row. See the related post Find a table containing specific text

Resources