Mechanize Traverse & Parse - mechanize-ruby

I'm trying to walk through a hitlist of pages found by Mechanize. Upon a search, which is working just fine, I get a hitlist with 10 records per page. A bottom navigation system takes me to the record count > 10. Display 10 per page. So 53 records = 6 "group pages" as I'm calling them.
What I want to do is use the top search results page to do the following:
Grab the html behind every record on this list. I can do that through an iteration.
Follow through the '[Next]' link at the bottom and repeat both 1 and 2 until there is no more 2. Essentially this will get every record.
I'm having an issue with moving off the first page into the second page. I'm grabbing the html behind the first 10 records, but then the system bonks on me. Not sure why. I thought I was iterating through the group pages, but it isn't advancing past the first group page.
counter = 1
puts "Counter: #{counter}"
while agent.page.links_with(:text => '[Next]').count == 1
page = agent.page.link_with(:text => '[Next]').click
puts "Counter/Next: #{counter} / #{agent.page.links_with(:text => '[Next]').count}"
agent.page.links_with(text: '[complete profile]').each do |link|
a = link.click # link.click goes to each company page
r = a.body.to_s
r = r.gsub(/^\s+\n/, "")
# enter company into db
class Company < ActiveRecord::Base
end
company = Company.new
company.cdate = DateTime.now
company.status = 'new'
company.requestID = req_id
company.html = r
company.save
end
counter += 1
end
Any insight appreciated. I know I'm close.
Cheers

Solution: move the click method behind page:
page = agent.click(page.link_with(:text => "[Next]"))

Related

Updating fields from changes in scroll levels

I have a page that has 3 levels. Levels 0 & 1 are from the same record. Level 2 is from a second record.
When a change is made to level 1, I would like to apply that change to the same field in Level 2's record.
Basically, this deals with EFF_STATUS in peoplesoft. If an effective row gets added to the record, and the EFF_STATUS is changed to Active or Inactive, I'd like to update the EFF_STATUS in my second record to match.
Here is the code I'm trying to execute and it is giving me an error of.. "Invalid row number 2 for class Rowset method GetRow. (2,263) K_OFFNSV_REC_EX.EFF_STATUS.SaveEdit PCPC:267 Statement:8 "
If %Component = Component.K_OFFNSV_CMP Then
Local Rowset &LEVEL0, &Level1, &Level2;
Local Row &L1Row, &L2Row;
Local number &I, &J;
&LEVEL0 = GetLevel0();
&Level1 = &LEVEL0(1).GetRowset(Scroll.K_OFFNSV_REC);
&I = CurrentRowNumber();
&L1Row = &Level1(&I);
If &L1Row.IsNew Then
&L1Row.K_OFFNSV_REC.LASTUPDDTTM.Value = %Date;
&L1Row.K_OFFNSV_REC.OPRID.Value = %UserId;
End-If;
&Level2 = &L1Row.GetRowset(Scroll.K_OFFNSV_REC_EX);
For &J = 1 To &Level2.ActiveRowCount
&L2Row = &Level2(&J);
&L2Row.K_OFFNSV_REC_EX.EFFDT.Value = %Date;
&L2Row.K_OFFNSV_REC_EX.EFF_STATUS.Value = &L1Row.K_OFFNSV_REC.EFF_STATUS.Value;
End-For;
End-If;
A suggestion, change/set values on SavePreChange. SaveEdit should be use for validations only.
With that being said:
Your currentrownumber returns the current row, so probably it is returning the row #2 on the level #2.
You need CurrentRowNumber(1) to get the #1 level.
Also, why are you setting the EFFDT yourself on the save? Look at other peoplesoft pages, you will see it is populated on the add by PS itself.

Button on click event disable button after 5 times it was clicked

I am fairly new to VB.NET. I feel like I did the hard parts and struggling with the easy one! I googled it before coming here but still struggling.
Basically I have a drop down list with all the available treatments and button next to it ( Add treatment). Every time I choose treatment from list I click on the button and it add it then bind it to gridview, the only issue is I want users to be able to add up to 5 treatments then disable the button. The question how Can I say find the number of times the button was clicked then I suppose I can just put If statement, I dont know how to find the value of the number of times the button was clicked.
Dim availableTreatment As ListItem = New ListItem
Dim count As Integer = 0
For count = 0 To 4
If count <= 4 And btnavailableTreatment ( button clicked value is what should go here) Then
availableTreatment = DDTreatmentList.SelectedItem
c.name = availableTreatment.Value
saveTreatment(c)
gvavailableTreatment.DataSource = getTreatment(c.name)
gvavailableTreatment.DataBind()
Else
btnavailableTreatment.Enabled = False
End If
count = count + 1
Next
Add this markup to your page:
<asp:HiddenField id="NumberOfTimesClickedHiddenField"
value="0"
runat="server"/>
Add this to your button click handler
'get previous value
Dim numberOfTimesClicked = Integer.Parse(NumberOfTimesClickedHiddenField.Value)
'increment value
numberOfTimesClicked += 1
'store new value
NumberOfTimesClickedHiddenField.Value = numberOfTimesClicked.ToString()
If numberOfTimesClicked >= 5 Then
btnavailableTreatment.Enabled = False
End If
Now the value will be persisted in the form data on the page.
I originally wrote this in C#, and I'm not a VB.NET programmer so I used a translator to convert it. There may be syntax errors but the general flow should be correct.

Second Dynamic DIV over writes first dynamic DIV

I have searched for almost three days now and I can't find an answer to this question. As I am somewhat new to ASP.Net I am not sure what the problem is.
Here is the situation. I have an aspx page that has a web form on it. When this form loads it has two SQL queries that execute and populate different controls.
My problem lies not with the queries but creating dynamic DIV tags and adding controls to them. If the query returns one or no records everything is fine. but if there are 2 or more records returned I only end up with the last record being displayed.
When I view the page source (from the browser) I can see the DIV tag ID that was created last, but the first DIV tag is gone. It is like the first DIV tag gets over written or something. I have no idea as to why or what is causing this.
Can anyone help please?
Here is my code:
While (incCounter <= rowCount)
DivName.ID = ("rev" & Convert.ToString(incCounter)) 'Create Div name of rev and the current counter number ex. rev1
review_comments.Controls.Add(DivName) 'Add DIV to web form
rName = (rName & Convert.ToString(incCounter)) 'Create name control name
rDate = (rDate & Convert.ToString(incCounter)) 'Create date control name
rComments = (rComments & Convert.ToString(incCounter)) 'Create comment control name
DivName.Controls.Add(New LiteralControl("<br />"))
DivName.Controls.Add(rRevByLabel)
rNameTextBox.ID = rName
DivName.Controls.Add(rNameTextBox)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(rDateLabel)
DivName.Controls.Add(rDateTextBox)
DivName.Controls.Add(New LiteralControl("<br />"))
DivName.Controls.Add(rCommentLabel)
DivName.Controls.Add(rCommentsTextBox)
'Assign data from query to new textboxes
rNameTextBox.Text = dtR.Rows(incCounter - 1)("reviewed_by_name").ToString()
rDateTextBox.Text = dtR.Rows(incCounter - 1)("reviewed_by_date").Date()
rCommentsTextBox.Text = dtR.Rows(incCounter - 1)("review_comments").ToString()
rName = "revName"
rDate = "rDate"
rComments = "revComments"
incCounter += 1
End While
I know all the variables are set up correctly because it shows me the last record.
What stupid thing am I doing?

Combo Box Filter Based on Value in Hidden Form

After a successful logon information about the user is held in a always open but hidden form.
Within the program there are various dropdowns that I want to filter based on 'txt_security' from the hidden form.
Row Source Example
SELECT tbl_master_ship.master_ship_id, tbl_master_ship.admin_only
FROM tbl_master_ship;
Example of data is there are 5 total ships, only one is admin_only. If txt_security = 1 then show all 5 ships else hide the one record marked admin_only.
I tried this but it only shows the one record if txt_security = 1.
WHERE (((tbl_master_ship.admin_only)=IIf([forms]![frm_global_variables]![txt_security]=0,True,False)))
This should be the easiest way:
WHERE [Forms]![frm_global_variables]![txt_security] = 1
OR tbl_master_ship.admin_only = 0
Only if [txt_security] <> 1 AND admin_only = True will the record not be shown.

If PageCount ist reached, start from First Page ( ASP Classic )

I have an Active Server Page, which displays Booking of the current Day. I setted the PageSize to two, so my display is displaying just 2 bookings per side, if there are more Records. So actually i have 8 bookings in my Recordset, so my ASP creates 4 Pages.
I wrote the following function:
Function getNext10(num)
getNext10 = CurrPage + 1
End Function
Finally i call that function in a meta tag, to automatically change the pages:
<meta http-equiv="refresh" content="10;URL=paging.asp?PageNo=<% Response.Write(getNext10(CurrPage))%>" />
It is working like charm.
But i have just one more Problem. If i do that like this the PageNo is increment endless.
My PageCount is 4.
So what i need in my function is a logic which checks whether the PageCount has been reached or not. If yes then he should start from the first page again, if not then increment until pagecount has reached.
Can someone help me with that? Thanks!!
EDIT:
I Wrote that function:
Function getNext10(num)
getNext10 = num
if getNext10 < i then // In `i`, i have my pagecount (4), which i got from Recordset.PageCount
// I checked it with Response.Write()
getNext10 = CurrPage + 1
End if
End Function
If i use i the if clause is not working, i dont know why. Its only working if i use directly a number.
If you know the page number will always be 4, then you can make a check for that.
Function getNext10(num)
if (num < i) then
CurrPage = num + 1
else
CurrPage = 1 'Reset the page count
end if
'Updating the variable used to call the page iterator
getNext10 = CurrPage
End Function
If you do not always know the page number then you will need to calculate the number of pages somehow to check against.

Resources