2

I have a worksite that I am trying to "browse".

Done it with this...

$login = Invoke-WebRequest -Uri 'http://www.privateworksite.com' -SessionVariable GetIP
$form = $login.Forms[0]
$Form.Fields['ctl00$ContentPlaceHolder1$RadTextBox7']= "*******"  <-- User ID goes here
$Form.Fields['ctl00$ContentPlaceHolder1$RadButton1'] = "submit"  <-- Submit button
$afterfirstclick Invoke-WebRequest -Uri 'http://www.privateworksite.com' -WebSession $GetIP -Method POST -Body $form.Fields

Now that I am at the page I need to click on a new button that appears. I have tried to add a -sessionvariable $afterfirstclick but I get the dreaded PS Red lines stating that you cannot have -WebSession and -SessionVariable together.

ADDED INFO: So how do I click on the button that is produced in $afterfirstclick? This is what I have for a button after the first click return.

ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl04$GECBtnExpandColumn  <-- a new button to create 
a drop down table with the info I need. The table is not there prior to the 
clicking the button so I need to click this button.

I have been all over and ever demo/post is about logging in to a site but after that how do you move around the site?

I have tried to feed $afterfirstclick into the -WebSession, bust... Also tried to repeat the whole thing with the next Field to click the button and that just results in a new site not the "continuation" of the current.

Where to go from here?

Oh and the URL never changes so I cannot submit VIA URL.

reddragon72
  • 191
  • 1
  • 3
  • 16
  • `URL never changes so I cannot submit VIA URL.` That is why using Invoke-WebRequest is not likely going to work for this. It "sounds" as if the button click is executing some javascript (perhaps an AJAX call) or it's manipulating the DOM. What IWR gives you is the HTML and the DOM but it provides no facility to execute JavaScript (like a browser does). Check out this SO question - http://stackoverflow.com/questions/516027/c-sharp-httpwebrequest-and-javascript – Keith Hill Jan 09 '16 at 00:44
  • you might want to inspect the HTTP request and construct it "manually" – Jaqueline Vanek Jan 09 '16 at 05:14
  • ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl04$GECBtnExpandColumn <-- a new button to create a drop down table with the info I need. The table is not there prior to the clicking the button so I need to click this button. – reddragon72 Jan 11 '16 at 13:06

1 Answers1

4

Ok found the solution! I needed to feed $afterfirstclick into the next invoke-webrequest. I feel so stupid that it was not that obvious.

End result...

$url = "http://private.com"  #<-- setup the URL
$tool = Invoke-WebRequest -Uri 'http://private.com' -UseDefaultCredentials -SessionVariable fb #<--create the site in a variable

write-host "1111111111111111" #<--just a marker to seperate the two requests so I can see the form.fields clearly
$Form = $tool.Forms[0] #<--first invoke form.fields
$Form.Fields #<--just printing them so I can see them
$Form.Fields['ctl00$ContentPlaceHolder1$RadTextBox7']= "USERID" #<--entering USERID into the form
$Form.Fields['ctl00$ContentPlaceHolder1$RadButton1'] = "submit" #<--clicking the submit button

$mainPage = Invoke-WebRequest  ("$url" + $Form.Action) -WebSession $fb -Body $Form.Fields -Method Post #<--create the new page by calling the page with the fields
$mainPage.RawContent | out-file h:\response.html #<--sending it to a file so I can see the first page and second page side by side

write-host "2222222222222222" #<--just a marker to seperate the two requests so I can see the form.fields clearly
$Form2 = $mainPage.Forms[0] #<--pulling the new page form.fields NOTICE I am using the previous variable for the first invoke
$Form2.Fields #<--printing fields to take a look at what needs to be included in the next round
$Form2.Fields['ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl04$GECBtnExpandColumn'] = "submit" #<--The newly generated button that needed to be clicked to continue

$mainPage2 = Invoke-WebRequest  ("$url" + $Form2.Action) -WebSession $fb -Body $Form2.Fields -Method Post -UseDefaultCredentials #<--OK BIG ONE!! First the form.field needs to be correct as well as using the original -websession!!!
$mainPage2.RawContent | out-file h:\response1.html #<--output the second resulting page to compare

You have to watch that second request. It must use the first invoke to create new form.fields but still use the original -websession variable.

No idea why but this works this way, but I used this for another site to go deep into it so it is solid, just keep repeating the chain. You could even use a foreach loop with all the known form.fields.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
reddragon72
  • 191
  • 1
  • 3
  • 16